由于某些原因,我在运行代码时会不断出现参数错误。
我正在运行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中给出的字符串
注意:第13行的C:\ xampp \ htdocs \ Resgistration \ server.php中的未定义索引:password_1
警告: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中给出的字符串
答案 0 :(得分:0)
如我的评论中所述,我想在您当前的代码中更改三件事,
mysqli_real_escape_string()
的调用的参数顺序错误-应该是
$username = mysqli_real_escape_string($db, $_POST['username']);
md5()
来哈希密码,这是非常不安全的-请改为使用password_hash()
(然后在登录时使用password_verify()
进行验证)。//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();
}
}
答案 1 :(得分:0)
我认为您需要改善您的php语法。 首先,不要在您的代码中使用MySQLi。
第二,您在插入查询中给定的值不同于上面为表单输入值定义的变量。
您需要更正它。