PHP! How to execute two different insert statements in a php file

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.