PHP form data written to MySQL code

Source: Internet
Author: User
Tags mysql code php form

12<?PHP3     if(!isset ($_post['Submit'])){  4         //If there is no form submission, a form is displayed5?>6<form action=""Method="Post">7Country: <input type="text"Name="Country"/>8Animal Name (English): <input type="text"Name="Animal"/>9Animal Name (Chinese): <input type="text"Name="CNAME"/>Ten<input type="Submit"Name="Submit"Value="Submit Form"/> One</form> A<?PHP -     }   -     Else   the     {   -         //If you submit the form -         //Database Connection Parameters -$host ="localhost";  +$user ="Root";  -$pass ="zq19890319";  +$db ="Phpdev";  A           at         //how to ask Hovertree.com -         //get the values in the form, check that the values in the form conform to the criteria, and do the appropriate escaping to prevent SQL injection -$country = Empty ($_post['Country'])? Die"Please enter a country name"):   -Mysql_escape_string ($_post['Country']);  -$animal = Empty ($_post['Animal'])? Die"Please enter the English name"):   -Mysql_escape_string ($_post['Animal']);  in$cname = Empty ($_post['CNAME'])? Die"Please enter Chinese name"):   -Mysql_escape_string ($_post['CNAME']);  to            +         //Open a database connection -$connection = mysql_connect ($host, $user, $pass) or Die ("Unable to connect!");  the            *         //Select Database $mysql_select_db ($db) or Die ("Unable to select database!"); Panax Notoginseng            -         //constructs a SQL query the$query ="INSERT into symbols (country, animal, CNAME) VALUE (' $country ', ' $animal ', ' $cname ')";  +            A         //Execute the query the$result = mysql_query ($query) or Die ("Error in query: $query.". Mysql_error ());  +            -         //when the insert operation succeeds, displays the record number of the inserted record $Echo"The record has been inserted, mysql_insert_id () =". mysql_insert_id ();  $            -         //Close the current database connection - mysql_close ($connection);  the     }   -?>

Reference: http://www.cnblogs.com/roucheng/p/phpmysql.html

The above code in a different format, do not know which format is better

12<?PHP3     if(!isset ($_post['Submit'])){  4         //If there is no form submission, a form is displayed5?>6<form action=""Method="Post">7Country: <input type="text"Name="Country"/>8Animal Name (English): <input type="text"Name="Animal"/>9Animal Name (Chinese): <input type="text"Name="CNAME"/>Ten<input type="Submit"Name="Submit"Value="Submit Form"/> One</form> A<?PHP -     }   -     Else   the     {   -         //If you submit the form -         //Database Connection Parameters -$host ="localhost";  +$user ="Root";  -$pass ="zq19890319";  +$db ="Phpdev";  A            at         //get the values in the form, check that the values in the form conform to the criteria, and do the appropriate escaping to prevent SQL injection -$country = Empty ($_post['Country'])? Die"Please enter a country name"):   -Mysql_escape_string ($_post['Country']);  -$animal = Empty ($_post['Animal'])? Die"Please enter the English name"):   -Mysql_escape_string ($_post['Animal']);  -$cname = Empty ($_post['CNAME'])? Die"Please enter Chinese name"):   inMysql_escape_string ($_post['CNAME']);  -            to         //Open database connection hovertree.com asked +$connection = mysql_connect ($host, $user, $pass) or Die ("Unable to connect!");  -            the         //Select Database *mysql_select_db ($db) or Die ("Unable to select database!");  $           Panax Notoginseng         //constructs a SQL query -$query ="INSERT into symbols (country, animal, CNAME) VALUE (' $country ', ' $animal ', ' $cname ')";  the            +         //Execute the query A$result = mysql_query ($query) or Die ("Error in query: $query.". Mysql_error ());  the            +         //when the insert operation succeeds, displays the record number of the inserted record -Echo"The record has been inserted, mysql_insert_id () =". mysql_insert_id ();  $            $         //Close the current database connection - mysql_close ($connection);  -     }   the?>

PHP form data written to MySQL code

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.