PHP form data written into the MySQL database code _ php instance

Source: Internet
Author: User
Tags php form
This article mainly introduces the related materials for writing PHP form data into the MySQL database. it is of great reference value. if you need it, you can refer to it and paste the code directly, the 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!

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.