SQL语法使用错误' 06:09:43 WHERE Book_id = 19 AND Date_issued = 2017-03-29 17:28:34'在第1行

时间:2017-03-30 04:24:38

标签: php mysql

  

错误:无法执行您的SQL语法有错误;   查看与您的MariaDB服务器版本对应的手册   正确使用语法#06; 09:43 WHERE Book_id = 19 AND   Date_issued = 2017-03-29 17:28:34'在第1行

我在执行以下代码时遇到了上述错误 -

if(isset($_GET['rt']))//rt is a variable I'm passing in an anchor tag
{
$dateis=$_GET['rt'];
}
if(isset($_GET['id']))//id is also sent along with rt
{
$bk_id=$_GET['id'];
}
$ret = 'returned successfully';
$date = date('Y-m-d H:i:s');
if(mysqli_query($conn, "UPDATE issued_books SET Date_returned = $date WHERE Book_id = $bk_id AND Date_issued = $dateis"))
{
    $fine = mysqli_query($conn, "SELECT DATEDIFF('$dateis', '$date') AS days");
    $fine = $fine*10;
    mysqli_query($conn, "UPDATE issued_books SET Fine_amt = (Fine_amt + $fine) WHERE Date_issued = $dateis");
    mysqli_query($conn, "UPDATE library_books SET Available = (Available + 1) WHERE Book_id = $bk_id");
    header("Location: return-books.php?book=".urlencode($ret));
} else{
    echo "ERROR: Could not able to execute " .mysqli_error($conn);
}

2 个答案:

答案 0 :(得分:0)

试试这些,只需在变量中添加单引号

"UPDATE issued_books SET Date_returned = '$date' WHERE Book_id = '$bk_id' AND Date_issued = '$dateis'"

"UPDATE issued_books SET Fine_amt = (Fine_amt + '$fine') WHERE Date_issued = '$dateis'"

"UPDATE library_books SET Available = (Available + 1) WHERE Book_id = '$bk_id'"

虽然使用预准备语句会更好。

答案 1 :(得分:0)

在您的第一次更新查询中,请使用'单引号

 Date_returned = '$date'

我还建议使用pdo