SQL语法错误或服务器错误?

时间:2014-08-02 16:12:19

标签: php mysql syntax-error

嘿伙计们,我是整个数据库场景的新手,并尝试执行一项相对简单的任务,但显然我做错了什么。每次我尝试执行此语句时,都会收到1064错误,告诉我语法错误或服务器版本太旧。 SQL Server版本是5.1.x,我正在运行PHP5。

这是我的代码:

$query = "INSERT INTO `cut_log` (`driver`, `date1`, `time`, `cut`, `flood`, `notes`) VALUES ($driver, $date, $time, $cut, $flood, $notes)";
$result = $mysqli->query($query);
if($result) {
echo "success";
} else {
echo "" . $mysqli->errno . $mysqli->error;
}

2 个答案:

答案 0 :(得分:6)

您的字符串值缺少引号:

$query = "INSERT INTO `cut_log` (`driver`, `date1`, `time`, `cut`, `flood`, `notes`) VALUES ('$driver', '$date', '$time', '$cut', '$flood', '$notes')";

答案 1 :(得分:2)

像约翰所说的那样,问题在于缺少引号 你应该做的是prepareavoid SQL injection攻击的查询:

$query = "INSERT INTO `cut_log` (`driver`, `date1`, `time`, `cut`, `flood`, `notes`) 
          VALUES (?, ?, ?, ?, ?, ?)";
if ($stmt = $mysqli->prepare($query)) {

    $stmt->bind_param("ssssss", $driver, $date, $time, $cut, $flood, $notes);

    if($stmt->execute()) {
      echo "success";
    } else {
      echo "" . $mysqli->errno . $mysqli->error;
    }

}