简体   繁体   English

PHP中的MYSQL语法错误,但SQL有效

[英]MYSQL Syntax error in php but sql is valid

I'm trying to start a transaction is mysql and insert data into the database. 我正在尝试使用mysql启动事务并将数据插入数据库。 The database source sql can be found on github here . 数据库源sql可以在github上找到 Here is the error: 这是错误:

Error: START TRANSACTION; 错误:开始交易; INSERT INTO Books(Title, PublicationDate, PurchaseDate, Description, LocationID, GenreID) VALUES('Simple Genius', '2008-4-1','2009-5-7','','Hardbook Library','Fiction'); 插入书籍(标题,出版日期,购买日期,描述,LocationID,GenreID)值('Simple Genius','2008-4-1','2009-5-7',','Hardbook Library','Fiction' ); SET @bookid = LAST_INSERT_ID(); SET @bookid = LAST_INSERT_ID(); INSERT INTO BookAuthors(FirstName, MiddleName, LastName) VALUES('David', '', 'Baldacci'); 插入BookAuthors(FirstName,MiddleName,LastName)VALUES('David','','Baldacci'); SET @authorid = LAST_INSERT_ID(); SET @authorid = LAST_INSERT_ID(); INSERT INTO AuthorsInBooks(AuthorID, BookID) VALUES(@authorid, @bookid); 插入AuthorsInBooks(AuthorID,BookID)VALUES(@authorid,@bookid); COMMIT; 承诺; You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO Books(Title, PublicationDate, PurchaseDate, Description, LocationID,' at line 3 检查与您的MySQL服务器版本相对应的手册,以获取在第3行“ INSERT INTO Books(标题,PublicationDate,PurchaseDate,Description,LocationID”附近)附近使用的正确语法。

Near 'INSERT INTO Books(Title, PublicationDate, PurchaseDate, Description, LocationID,' doesn't make sense to me because it is missing GenreID after LocationID. Am i missing something? When I copy and paste this code into phpmyadmin it works fine. My php version is 5.4. “在INSERT INTO Books(标题,出版物日期,购买日期,描述,位置ID”附近)对我来说没有意义,因为在位置ID之后缺少GenreID。我是否丢失了某些东西?当我将此代码复制并粘贴到phpmyadmin中时,它可以正常工作。我的PHP版本是5.4。

Here is php code: 这是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);

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

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

In response to your comment " Can I see an example of what you mean @eggyal ? ": 在回应您的评论我可以看到您的意思@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);

Note that I've not included above any error detection/handling, which you'd certainly want to include in any real-world code. 请注意,我没有将任何错误检测/处理都包含在上面,您当然希望将其包含在任何实际代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM