PHP form data written to the MySQL database code, php form mysql database _ PHP Tutorial

Source: Internet
Author: User
PHP form data is written into the MySQL database code, and php form mysql database. The PHP form data is written into the MySQL database code. The php form mysql database does not talk much about it. you can directly post code to everyone. the specific code is as follows: h insert operation hphpif (! Isset (PHP form data written to the MySQL database code, php form mysql database

If you don't talk much about it, paste the code directly. the specific code is as follows:

 
  
<? Php} else {// if the form is submitted // database connection parameter $ host = "localhost"; $ user = "root"; $ pass = "zq "; $ db = "phpdev"; // why do I ask hovertree.com//obtain the value in the table list, check the value in the table list to conform to the standard, and make it right to prevent sqlinjection $ country = empty ($ _ POST ['country'])? Die ("enter country name"): mysql_escape_string ($ _ POST ['country']); $ animal = empty ($ _ POST ['Animal '])? Die ("Enter the English name"): mysql_escape_string ($ _ POST ['Animal ']); $ cname = empty ($ _ POST ['cname'])? Die ("Enter Chinese name"): mysql_escape_string ($ _ POST ['cname']); // open the database connection $ connection = mysql_connect ($ host, $ user, $ pass) or die ("Unable to connect! "); // Select the database mysql_select_db ($ db) or die (" Unable to select database! "); // Construct an SQL query $ query =" INSERT INTO symbols (country, animal, cname) VALUE ('$ country', '$ animal', '$ cname ') "; // execute this query $ result = mysql_query ($ query) or die (" Error in query: $ query. ". mysql_error (); // after the insert operation is successful, the record number echo "the record has been inserted, mysql_insert_id () = ". mysql_insert_id (); // close the current database connection mysql_close ($ connection) ;}?>
 

The above code uses different formats. I don't know which format is better.

 
  
<? Php} else {// if the form is submitted // database connection parameter $ host = "localhost"; $ user = "root"; $ pass = "zq "; $ db = "phpdev"; // obtain the value in the form, check whether the value in the form complies with the standard, and perform proper escape, prevent SQL injection $ country = empty ($ _ POST ['country'])? Die ("enter country name"): mysql_escape_string ($ _ POST ['country']); $ animal = empty ($ _ POST ['Animal '])? Die ("Enter the English name"): mysql_escape_string ($ _ POST ['Animal ']); $ cname = empty ($ _ POST ['cname'])? Die ("Enter Chinese name"): mysql_escape_string ($ _ POST ['cname']); // why does it ask $ connection = mysql_connect ($ host, $ user, $ pass) or die ("Unable to connect! "); // Select the database mysql_select_db ($ db) or die (" Unable to select database! "); // Construct an SQL query $ query =" INSERT INTO symbols (country, animal, cname) VALUE ('$ country', '$ animal', '$ cname ') "; // execute this query $ result = mysql_query ($ query) or die (" Error in query: $ query. ". mysql_error (); // after the insert operation is successful, the record number echo "the record has been inserted, mysql_insert_id () = ". mysql_insert_id (); // close the current database connection mysql_close ($ connection) ;}?>
 

The above is the code for writing PHP form data into the MySQL database. I hope it will help you!

I don't need to talk about it anymore. I just paste the code directly. the specific code is as follows: h insert operation/h php if (! Isset (...

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.