PHP - 插入值错误:检查SQL版本

时间:2016-05-25 07:13:55

标签: php mysql

我试图将一些值插入数据库但它不起作用。它显示错误。

  

您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在第1行'','pranavkumar.chess @ gmail.com')'附近使用正确的语法

我需要做点什么。那么,你可以帮忙吗。

PHP Code的部分是:

$reason= $_GET['reason'];      
$option= $_GET['option'];
$for= $_GET['category'];
$approved= $_GET['approved'];
$before= $_GET['before'];
$username= $_SESSION ["username"];
     $myquery="INSERT INTO request (`serial number`, `request date`, `request for category`, `request`, `reason`, `approved by`, `username`) VALUES  ('',NOW(),'$for','$option','$reason',$approved','$username')";
     $query = mysql_query($myquery);

     if ( ! $query ) {
                     echo mysql_error();
                     die;
  }

3 个答案:

答案 0 :(得分:1)

这是因为您在$ approved变量之前错过了报价。

试试这个:

$reason= $_GET['reason'];      
$option= $_GET['option'];
$for= $_GET['category'];
$approved= $_GET['approved'];
$before= $_GET['before'];
$username= $_SESSION ["username"];
$myquery= "INSERT INTO request (`serial number`, `request date`, `request for category`, `request`, `reason`, `approved by`, `username`) VALUES  ('',NOW(),'$for','$option','$reason','$approved','$username')";
     $query = mysql_query($myquery);

     if ( ! $query ) {
                     echo mysql_error();
                     die;
  }

答案 1 :(得分:1)

单一报价缺失@' $ approved'

     $myquery="INSERT INTO request (`serial number`, `request date`, `request for category`, `request`, `reason`, `approved by`, `username`) VALUES  ('',NOW(),'$for','$option','$reason','$approved','$username')";

答案 2 :(得分:1)

首先,使用PDO:

$stmt = $con->prepare("INSERT INTO request (`serial number`, `request date`, `request for category`, `request`, `reason`, `approved by`, `username`) VALUES (?, NOW(), ?, ?, ?, ?, ?)");
$stmt->execute([
    '',
    $for,
    $option,
    $reason,
    $approved,
    $username
]);

第二,在:

('',NOW(),'$for','$option','$reason',$approved','$username')";

变化:

',$approved','

这就是原因:

','$approved','