不了解 SQL 参数



由于某种原因,当我运行代码时,我不断收到参数错误。

我正在运行XAMP,Atom,mySql和PhpMyAdmin。 我意识到也许与我正在使用的事实有关

mysql_real_escape_string

不再支持。所以我把它改成了mysqli,但现在它显示了一个不同的错误。

我是整个编程领域的新手,所以我对一切都很落后。

$username = "";
$email = "";
$errors = array();
//connect to the database
$db = mysqli_connect('localhost', 'root', '', 'regist');
//if the register is clicked
if (isset($_POST['register'])) {
$username = mysqli_real_escape_string($_POST['username'], $db);
$email = mysqli_real_escape_string($_POST['email'], $db);
$password_1 = mysqli_real_escape_string($_POST['password_1'], $db);
$password_2 = mysqli_real_escape_string($_POST['password_2'], $db);
//ensure the form fields are filled properly
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($password_1)) {
array_push($errors, "Password is required");
}
if ($password_1 != $password_2) {
array_push($errors, "The two password do not match");
}
//if there are no errors, save user to database
if (count($errors)==0) {
$password = md5($password_1); //encrypt password before storing in database
$sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
mysqli_query($db, $sql);
}
}

我希望它将详细信息注册到数据库中,而是收到下面列出的错误。

警告:mysqli_real_escape_string() 期望参数 1 是 mysqli,字符串在第 11 行的 C:\xampp\htdocs\Resgistration\server.php 中给出

警告:mysqli_real_escape_string() 期望参数 1 是 mysqli,字符串在第 12 行的 C:\xampp\htdocs\Resgistration\server.php 中给出

注意:未定义的索引:password_1 C:\xampp\htdocs\Resgistration\server.php第 13 行

警告:mysqli_real_escape_string() 期望参数 1 为 mysqli,空值在第 13 行的 C:\xampp\htdocs\Resgistration\server.php 中给出

警告:mysqli_real_escape_string() 期望参数 1 是 mysqli,字符串在第 14 行的 C:\xampp\htdocs\Resgistration\server.php 中给出

正如我的评论中提到的,我想在您当前的代码中更改三件事,

  1. 您对mysqli_real_escape_string()的调用具有错误的参数顺序 - 它应该是
    $username = mysqli_real_escape_string($db, $_POST['username']);
    
  2. 您正在使用md5()对密码进行哈希处理,这是非常不安全的 - 请改用password_hash()(然后使用您登录password_verify()进行验证)。
  3. 不要打扰 1.,而是使用预准备语句。
//connect to the database
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli('localhost', 'root', '', 'regist');
$db->set_charset("utf8");
//if the register is clicked
if (isset($_POST['register'])) {
//ensure the form fields are filled properly
if (empty($_POST['username'])) {
array_push($errors, "Username is required");
}
if (empty($_POST['email'])) {
array_push($errors, "Email is required");
}
if (empty($_POST['password_1'])) {
array_push($errors, "Password is required");
}
if ($_POST['password_1'] != $_POST['password_2']) {
array_push($errors, "The two password do not match");
}
if (!count($errors)) {
$password = password_hash($_POST['password_1'], PASSWORD_DEFAULT);
$stmt = $db->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $_POST['username'], $_POST['email'], $_POST['password_1']);
$stmt->execute();
$stmt->close();
}
}

我认为你需要改进你的php语法。 首先,MySQL不要在你的代码中使用mysqli。

其次,您在插入查询中给出的值与您上面为表单输入值定义的变量不同。

您需要更正它。

最新更新