ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
提交表单时,我遇到了以上错误。
表格(HTML):
<form action="register.php" method="POST">
<input type="text" name="username" maxlength="25" class="txtbx" placeholder="Username"><br>
<input type="password" name="password" maxlength="40" class="txtbx" placeholder="Password"><br>
<input type="password" name="confirm_password" maxlength="40" class="txtbx" placeholder="Confirm Password"><br>
<input type="text" name="email" maxlength="50" class="txtbx" placeholder="Email"><br>
<input type="text" name="confirm_email" maxlength="50" class="txtbx" placeholder="Confirm Email"><br>
<input type="submit">
</form>
PHP:
$connect=mysqli_connect("[host (snip)]", "[username (snip)]", "[password (snip)", "[database name (snip)]");
if (mysqli_connect_errno())
{
echo "<script>alert('Error connecting to MySQL:' . mysqli_connect_error())</script>";
}
$username=mysqli_real_escape_string($connect, $_POST['username']);
$password=mysqli_real_escape_string($connect, $_POST['password']);
$password2=mysqli_real_escape_string($connect, $_POST['confirm_password']);
$email=mysqli_real_escape_string($connect, $_POST['email']);
$email2=mysqli_real_escape_string($connect, $_POST['confirm_email']);
if($password!=$password2)
{
echo "<script>alert('The password and the password confirmation do not match.');</script>";
}
if($email!=$email2)
{
echo "<script>alert('The email and the email confirmation do not match.');</script>";
}
if($username && $password == $password2 && $email == $email2)
{
$addto=mysqli_query($connect,"INSERT INTO users (username, password, email)
VALUES ('$username', '$password', '$email')");
if (!mysqli_query($connect,$addto))
{
die('ERROR: ' . mysqli_error($connect));
}
else
{
echo "Successful!";
}
}
mysqli_close($connect);
我不明白这个错误。 PHP代码的第一行没有字符“ 1”。应该是
mysql
而不是mysqli
吗?谢谢,
〜哼
最佳答案
如注释中所述,您需要将代码if (!mysqli_query($connect,$addto))
更改为if ($addto === false)
。这是因为$addto
可以是混合值,因此使用===可以查找没有类型转换的布尔值false
。它将解决您的问题。
有关mysqli_query可以返回的内容的文档,请click here并向下滚动以返回值。