【问题标题】:MYSQL Syntax error in php but sql is validphp中的MYSQL语法错误但sql有效
【发布时间】:2016-01-15 18:18:41
【问题描述】:

我正在尝试启动一个事务是 mysql 并将数据插入数据库。数据库源sql可以在githubhere找到。这是错误:

错误:开始交易;插入书籍(标题、出版日期、 PurchaseDate, Description, LocationID, GenreID) VALUES('简单 Genius', '2008-4-1','2009-5-7','','Hardbook Library','Fiction');放 @bookid = LAST_INSERT_ID();插入图书作者(名字, MiddleName, LastName) VALUES('David', '', 'Baldacci');设置@authorid = LAST_INSERT_ID(); INSERT INTO AuthorsInBooks(AuthorID, BookID) 值(@authorid,@bookid);犯罪;您的 SQL 中有错误 句法;检查与您的 MySQL 服务器版本相对应的手册 在 'INSERT INTO Books(Title, PublicationDate、PurchaseDate、Description、LocationID,'在第 3 行

'INSERT INTO Books(Title, PublicationDate, PurchaseDate, Description, LocationID,' 对我来说没有意义,因为它在 LocationID 之后缺少 GenreID。我错过了什么吗?当我将此代码复制并粘贴到 phpmyadmin 中时工作正常。我的 php 版本是 5.4。

这里是php代码:

$sql = "
START TRANSACTION;

INSERT INTO Books(Title, PublicationDate, PurchaseDate, Description, LocationID, GenreID)
VALUES('".$Title."', '".$YearWritten."','".$YearPurchased."','".$Description."','".$Location."','".$Genre."');

SET @bookid =  LAST_INSERT_ID();

INSERT INTO BookAuthors(FirstName, MiddleName, LastName)
VALUES('".$AuthFirstName."', '".$AuthMiddleName."', '".$AuthLastName."');

SET @authorid =  LAST_INSERT_ID();

INSERT INTO AuthorsInBooks(AuthorID, BookID)
VALUES(@authorid, @bookid);

COMMIT;
";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

【问题讨论】:

标签: php mysql php-5.4


【解决方案1】:

mysqli_query()只能执行1个查询,如果要执行多个查询,需要:

if (mysqli_multi_query($conn, $sql)) {

【讨论】:

  • 但与其这样做,不如使用 API 调用来管理事务并分别发送每个命令……当然,还要对这些变量进行参数化!
  • 完美运行。非常感谢!
  • @eggyal 我完全同意。
  • 我可以看一个例子来说明你的意思吗@eggyal?
  • 小心解释@user2864740
【解决方案2】:

回复your comment我可以看看你的意思的例子吗@eggyal ?”:

// mysqli provides API calls for managing transactions
mysqli_autocommit($conn, false);

// parameterise variables - NEVER concatenate them into dynamic SQL
$insert_book = mysqli_prepare($conn, '
  INSERT INTO Books
    (Title, PublicationDate, PurchaseDate, Description, LocationID, GenreID)
  VALUES
    (?, ?, ?, ?, ?, ?)
');

// bind the variables that (will) hold the actual values
mysqli_stmt_bind_param(
  $insert_book,
  'siisss', // string, integer, integer, string, string, string
  $Title, $YearWritten, $YearPurchased, $Description, $Location, $Genre
);

// execute the statement (you can change the values of some variables and
// execute repeatedly without repreparing, if so desired - much faster)
mysqli_stmt_execute($insert_book);

// mysqli provides API calls for obtaining generated ids of inserted records
$book_id = mysqli_insert_id($conn);

// ... etc ...

// use the API call to commit your transaction
mysqli_commit($conn);

// tidy up
mysqli_stmt_close($insert_book);

请注意,我没有在上面包含任何错误检测/处理,您肯定希望将其包含在任何实际代码中。

【讨论】:

    猜你喜欢
    • 2014-06-08
    • 2014-10-25
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 2011-08-08
    相关资源
    最近更新 更多