【问题标题】:MySQL Error "an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near"MySQL 错误“您的 SQL 语法错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解附近使用的正确语法”
【发布时间】:2016-05-06 10:42:35
【问题描述】:

在将数据插入数据库时​​,我从 MySQL 收到此错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“leave (apply_date, leave_category, leave_type, from_date, to_date, reason, duty_’ 附近使用正确的语法”

我以前没有收到过(我在一个多星期前做了同样的脚本,从来没有出现过这个错误。

下面是“func.php”文件的代码

function apply_leave($apply_date,$category,$type,$from,$to,$reason,$assign){

  $result=mysql_query("INSERT INTO leave (apply_date, leave_category, leave_type, from_date, to_date, reason, duty_assign)
 VALUES ('$apply_date', '$category', '$type', '$from', '$to', '$reason', '$assign')")or die("error on query".mysql_error());

下面是“leave_apply.php”文件的代码

if(isset($_POST['apply'])){
   $apply_date=$_POST['apply_date'];
   $category=$_POST['category'];
   $type=$_POST['type'];
   $from=$_POST['from'];
   $to=$_POST['to'];
   $no_of_date=$_POST['no_of_date'];
   $reason=$_POST['reason'];
   $assign=$_POST['assign'];
   $result=apply_leave($apply_date, $category, $type, $from, $to, $reason, $assign);

【问题讨论】:

  • 缺少数据库连接变量
  • 停止使用已弃用的mysql_* API。将mysqli_PDO与准备好的语句一起使用
  • 如果某个答案解决了您的问题,请将其标记为accepted

标签: php mysql


【解决方案1】:

leave 是一个reserved keyword in MySQL,需要用反引号转义。

INSERT INTO `leave` (...)

【讨论】:

    猜你喜欢
    • 2022-06-14
    • 2022-01-14
    • 2022-01-24
    • 2015-03-30
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多