PHP form data written to MySQL database code, PHP form MySQL database _php tutorial

Source: Internet
Author: User
Tags php form

PHP form data written to MySQL database code, PHP form MySQL database


Not much nonsense to say, directly to everyone to paste the code, the specific code is as follows:

 
  

<?php} else {//if the form//database connection parameter is submitted $host = "LocalHost"; $user = "root"; $pass = "ZQ"; $db = "Phpdev"; Why do you ask hovertree.com//get the values in the form, check that the values in the form conform to the standard, and do the appropriate escaping to prevent SQL injection $country = empty ($_post[' country ')? Die ("Please enter the country name"): mysql_escape_string ($_post[' country ')); $animal = Empty ($_post[' animal ')? Die ("Please enter English name"): mysql_escape_string ($_post[' animal ')); $cname = Empty ($_post[' cname '])? Die ("Please enter Chinese name"): mysql_escape_string ($_post[' cname '); Open database Connection $connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect!"); Select Database mysql_select_db ($db) or Die ("Unable to select database!"); Constructs a SQL query $query = "INSERT into symbols (country, animal, CNAME) VALUE (' $country ', ' $animal ', ' $cname ')"; Execute the query $result = mysql_query ($query) or Die ("Error in Query: $query.") Mysql_error ()); After the insert operation succeeds, the record number of the inserted record is displayed echo "record has been inserted, mysql_insert_id () =". mysql_insert_id (); Close the current database connection mysql_close ($connection); }?>

The code above is in a different format and does not know which format is better

 
  

<?php} else {//if the form//database connection parameter is submitted $host = "LocalHost"; $user = "root"; $pass = "ZQ"; $db = "Phpdev"; Get the values in the form, check that the values in the form conform to the standard, and do the appropriate escaping to prevent SQL injection $country = empty ($_post[' country ')? Die ("Please enter the country name"): mysql_escape_string ($_post[' country ')); $animal = Empty ($_post[' animal ')? Die ("Please enter English name"): mysql_escape_string ($_post[' animal ')); $cname = Empty ($_post[' cname '])? Die ("Please enter Chinese name"): mysql_escape_string ($_post[' cname '); Open database connection hovertree.com Ask $connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect!"); Select Database mysql_select_db ($db) or Die ("Unable to select database!"); Constructs a SQL query $query = "INSERT into symbols (country, animal, CNAME) VALUE (' $country ', ' $animal ', ' $cname ')"; Execute the query $result = mysql_query ($query) or Die ("Error in Query: $query.") Mysql_error ()); After the insert operation succeeds, the record number of the inserted record is displayed echo "record has been inserted, mysql_insert_id () =". mysql_insert_id (); Close the current database connection mysql_close ($connection); }?>

The above is a small series to introduce you to the PHP form data written to MySQL database code, I hope to help you!

http://www.bkjia.com/PHPjc/1133042.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133042.html techarticle PHP form data written to the MySQL database code, PHP form MySQL database nonsense to say, directly to everyone to paste the code, the specific code is as follows: H insert operation/php if (!isset .

  • Related Article

    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.