我终于做出了我认为是执行查询的好、安全和快速的方法,但我想在整个站点上实施它之前完全确定。
我的代码:
$email = $_POST['email'];
$displayName = $_POST['displayName'];
$pass = $_POST['pass1'];
if($stmt = $link -> prepare("INSERT INTO profiles (email, displayName, password) VALUES (?, ?, md5(?))")) {
/* Bind parameters
s - string, b - boolean, i - int, etc */
$stmt -> bind_param("sss", $email, $displayName, $pass);
/* Execute it */
$stmt -> execute();
echo "You are now registered.<br />";
echo "<a href=\"login.php\">Login</a>";
/* Close statement */
$stmt -> close();
}
顺便说一句,stmt 是什么意思/代表什么?
编辑,新代码:
/* Create a prepared statement */
$stmt = $link -> prepare("INSERT INTO profiles (email, displayName, password,
dateRegistered) VALUES (?, ?, md5(?), NOW())");
if ( false===$stmt ) {
die('prepare() failed: ' . htmlspecialchars($link->error));
}
$rc = $stmt -> bind_param("sss", $email, $displayName, $pass);
if ( false===$rc ) {
die('bind_param() failed: ' . htmlspecialchars($stmt->error));
}
/* Execute it */
$rc = $stmt->execute();
if ( false===$rc ) {
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
echo "You are now registered.<br />";
echo "<a href=\"login.php\">Login</a>";
/* Close statement */
$stmt -> close();