SQL 语法错误;检查与您的 MySQL 服务器版本相对应的手册

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21509026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:57:19  来源:igfitidea点击:

Error in your SQL syntax; check the manual that corresponds to your MySQL server version

mysqlsql

提问by Kasani Prabhakar

HTML CODE

代码

<!DOCTYPE html>
<html>
  <head>
    <style>
      #right
      {
      margin-top:109px;
      margin-right:225px;
      }
      #button
      {
      margin-right:;
      margin-top:22px;
      }
    </style>
  </head>
  <body>
    <div id="total" style="width:1350px">
      <div id="top" style="width:1350px;height:69px;background-image:url('top.png');float:;"></div>
      <div id="body" style="width:1350px;height:570px;background-image:url('body.jpg');float:;">
        <div id="right" style="background-color:;height:283px;width:320px;float:right;">
          <form action="new.php" method="post" >
            <input type="text" name="username" value="" size="37"  placeholder="username"style="height:20px"><br><br>
            <pre><input type="password" name="password" value="" size="37" placeholder="password"style="height:20px"> 
<div id="button"><input type="image" name="" value="submit" src="button.png" />  <input type="checkbox" value="check"><br></pre>
            </div>
          </form>
        </div>
      </div>
      <div id="footer" style="width:1350px;height:33px;background-image:url('footer.png');clear:both;"></div>
    </div>
  </body>
</html>

MY PHP CODE

我的 PHP 代码

<?php
$con=mysqli_connect("localhost","prabha","prabha","prabha");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO table (username, password)
VALUES
('$_POST[username]','$_POST[password]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>


When i clicked on submit button after entering username:prabha password:passwordofprabha

当我输入用户名后点击提交按钮时:prabha 密码:passwordofprabha

It shows the following error. Please fix my code.

它显示以下错误。请修复我的代码。

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 'table (username, password) VALUES ('prabha','passwordofprabha')' at line 1

错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的“table (username, password) VALUES ('prabha','passwordofprabha')' 附近使用的正确语法

回答by user1844933

Use ` backticks for MYSQL reserved words...

对 MYSQL 保留字使用 ` 反引号...

table name "table" is reserved word for MYSQL...

表名“table”是 MYSQL 的保留字...

so your query should be as follows...

所以你的查询应该如下...

$sql="INSERT INTO `table` (`username`, `password`)
VALUES
('$_POST[username]','$_POST[password]')";

回答by user2768411

Some special charactersgive this type of error, so use

一些特殊字符会导致此类错误,因此请使用

$query="INSERT INTO `tablename` (`name`, `email`)
VALUES
('$_POST[name]','$_POST[email]')";