PHP form data written to MySQL code, PHP form written mysql_php tutorial

Source: Internet
Author: User
Tags mysql code php form

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


Insert operation

Phpif(!isset($_post[' Submit '])){ //If there is no form submission, a form is displayed?> PHP}Else { //if the form//database connection parameters are submitted $host= "localhost"; $user= "Root"; $pass= "zq19890319"; $db= "Phpdev"; //what to ask hovertree.com//Get the values in the form, check whether the values in the form conform to the standards, and do the appropriate escaping to prevent SQL injection $country=Empty($_post[' Country '])? die("Please enter 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 a 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()); //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); } ?>

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

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

  1. Insert operation

  2. if (!isset ($_post[' Submit '])) {
  3. //If there is no form submission, a form is displayed
  4. ?>
  5. }
  6. Else
  7. {
  8. //If a form is submitted
  9. //Database connection Parameters
  10. $host = "localhost" ;
  11. $user = "Root" ;
  12. $pass = "zq19890319" ;
  13. $db = "Phpdev" ;
  14. //Get the values in the form, check whether the values in the form conform to the standards, and do the appropriate escaping to prevent SQL injection
  15. $country = Empty ($_post[' country '])? die ("Please enter country name"):
  16. Mysql_escape_string ($_post[' country ']);
  17. $animal = Empty ($_post[' animal '])? die ("Please enter English name"):
  18. Mysql_escape_string ($_post[' animal ']);
  19. $cname = Empty ($_post[' cname '])? die ("Please enter Chinese name"):
  20. Mysql_escape_string ($_post[' cname ']);
  21. //Open database connection hovertree.com ask
  22. $connection = mysql_connect ($host, $user, $pass) or die("Unable to connect!" );
  23. //Select Database
  24. mysql_select_db ($db) or die("Unable to Select database! " );
  25. //construct a SQL query
  26. $query = "INSERT into symbols (country, animal, CNAME) VALUE (' $country ', ' $animal ', ' $cname ')" ;
  27. //Execute the query
  28. $result = mysql_query ($query) or die("Error in Query: $query." . Mysql_error ());
  29. when the insert operation succeeds, the record number of the inserted record is displayed
  30. Echo "The record has been inserted, mysql_insert_id () =". mysql_insert_id ();
  31. //Close the current database connection
  32. Mysql_close ($connection);
  33. }
  34. ?>

http://www.bkjia.com/PHPjc/1130484.html www.bkjia.com true http://www.bkjia.com/PHPjc/1130484.html techarticle PHP form data written to MySQL code, PHP form written to MySQL H1 insert operation/h1? php if (! isset ($_post [' submit '])) {//If there is no form submission, show a form? Form action= ". ..

  • 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.