PHP help! How to execute two different insert statements in a php file? & Lt ;? Php & nbsp; session_start (); include ("Conn/conn. php "); $ ISBN =$ _ POST [" ISBN "]; $ B _Name =$ _ POST [" bookName "]; PHP help! How to execute two different insert statements in a php file?
Session_start ();
Include ("Conn/conn. php ");
$ ISBN = $ _ POST ["ISBN"];
$ B _Name = $ _ POST ["bookName"];
$ B _Author = $ _ POST ["B _Author"];
$ B _Pub = $ _ POST ["B _Pub"];
$ B _Author = $ _ POST ["B _Author"];
$ B _BuyTime = date ("Y-m-d ");
$ B _Num = $ _ POST ["B _Num"];
$ Content = $ _ POST ["content"];
Echo $ B _Num ."
"; // Test the output statement
Echo $ Content; // test the output statement
$ Sql1 = mysql_query ("insert into book1 (ISBN, B _Name, B _Author, B _Pub, B _BuyTime) values ('$ ISBN', '$ B _Name', '$ B _Author ', '$ B _pub',' $ B _BuyTime ')");
$ Sql2 = mysql_query ("insert into book2 (B _Num, ISBN, Borrowed, Ordered, Introduction) values ('$ B _Num', '$ ISBN', 'no', 'no ', '$ content ')");
Echo"
Alert ('Book info added successfully! '); History. back (); location. href = 'Book. php'; script ";
?>
This is the php file executed after the "submit" button is clicked on another php file. The $ B _Num and $ Content variables in the output test both have values. After Execution, check the mysql database. we can see that a row of data is successfully inserted in the book1 table, but no data is inserted in the book2 table. it seems that only the SQL 1 statement is executed, and the SQL 2 statement is not executed. The man can help me modify it, you can insert data into two different tables at the same time! PHP mysql insert statement two SQL statements
------ Solution --------------------
Is the output variable NULL?
$ Sql2 = mysql_query ("insert into book2 (B _Num, ISBN, Borrowed, Ordered, Introduction) values ('$ B _Num', '$ ISBN', 'no', 'no ', '$ content') ") or die (mysql_error ());
So there is no error message.
------ Solution --------------------
It must be your insert statement. First, check whether each val matches the field type, and then check whether non-empty fields are not assigned values.
Then, your SQL statement uses variables contained in single quotes. the content in double quotes can be interpreted and replaced, while the content in single quotes is always considered as common characters. If none of the above methods works, replace single quotes with double quotation marks.