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

时间:2014-02-02 09:32:08

标签: mysql sql

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>

我的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);
?>

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

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

  

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

2 个答案:

答案 0 :(得分:15)

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

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

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

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

答案 1 :(得分:2)

某些特殊字符会出现此类错误,因此请使用

#include <stdio.h>
#include <string.h>

int main(void) {
char a[2048][2048];
int i = 0, 
    j = 0, 
    k = 0, 
    n;

while(i < 2048 && fgets(a[i], 2048, stdin) != NULL)
{
    n = strlen(a[i]);
    if(n > 0 && a[i][n-1] == '\n')
    a[i][n -1] = '\0';
    i++;
}

for(j = 0; j < i; j++)
{
    char max[2048];
    strcpy (max,a[j]);
    for(k = j + 1; k < i; k++)
    {
        if(strcmp(a[k], max) < 0)
        {
            char temp[2048];
            strcpy(temp, a[k]);
            strcpy(a[k], max);
            strcpy(max, temp);
        }
    }
    strcpy(a[j],max);
}

for( j = 0; j < i; j++){
    printf("%s\n", a[j]);
}
return 0;
}