MySQL/SQL/PHP语法错误


Error in MySQL/SQL/PHP syntax

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吗?

谢谢,~ Hom

如注释中所述,您需要将代码if (!mysqli_query($connect,$addto))更改为if ($addto === false)。这是因为$addto可以是混合值,所以通过使用===来查找没有类型转换的布尔值false。它会解决你的问题。

关于mysqli_query可以返回什么的文档,点击这里并向下滚动到返回值

错误使用" and ":

echo "<script>alert('Error connecting to MySQL: " . mysqli_connect_error() ."')</script>";