PHP MySQL data save insert into

Source: Internet
Author: User
Tags html form sql tutorial php mysql

PHP MySQL data save insert into

The declaration that is inserted is used to insert a new record table.

Inserting data into a database table
The declaration that is inserted is used to add a new record to the database table.

Grammar
May be written in two forms of insert speech.

The first form of data without specific column names will be inserted, with only their values:

INSERT into table_name
VALUES (value1, value2, Value3,...)
  
  

The second form specifies the column name and values inserted:

INSERT into table_name (column1, Column2, Column3,...)
VALUES (value1, value2, Value3,...)
    
    

To learn more about SQL, please visit our SQL Tutorial.
In order for PHP to execute the above statement, we must use the mysql_query () function. This feature is used to send a MySQL connection to a query or command.

For example
In the previous chapter we also created a table named "People" with three columns; "Surname", "surname" and "era"

。 We will use the same table in this example. The following example adds two new records to the "People" Table:

<?php
$con = mysql_connect ("localhost", "Peter", "abc123");
if (! $con)
  {
  die (' Could not connect: '. Mysql_error ());
  }
mysql_select_db ("my_db", $con);
mysql_query ("INSERT into Persons (FirstName, LastName, age) 
VALUES (' Peter ', ' Griffin ', ' 35 ')");
mysql_query ("INSERT into Persons (FirstName, LastName, age) 
VALUES (' Glenn ', ' Quagmire ', ' 33 ')");
Mysql_close ($con);
? >

Inserting data from form to database
Now we will create an HTML form that can be used to add new records to "people" seated.

The following is an HTML form:

 
      
<form action= "insert.php" method= "POST" >
Firstname: <input type= "text" name= "Firstname"/>
Lastname: <input type= "text" name= "Lastname"/> Age
: <input type= "text" name= "age"/>
Type= "Submit"/>
</form>
</body>
 
        
          
When the user clicks on the Submit button HTML form in the example above, the data is sent to "insert.php".

The "insert.php" file connects to a database and retrieves the form of the value with PHP's $ _post variable.

Then the INSERT INTO statement executed by the mysql_query () function, and a new record is added to the person's seat.

This is the "insert.php" page:

 

 <?php $con = mysql_connect ("localhost", "Peter", "abc123"); if (!
  $con) {die (' could not connect: '. Mysql_error ());

  
 mysql_select_db ("my_db", $con); 
 $sql = INSERT into Persons (FirstName, LastName, age) VALUES (' $_post[firstname] ', ' $_post[lastname ', ' $_post[age ')

  ')";
 if (!mysql_query ($sql, $con)) {die (' Error: '. mysql_error ());

  echo "1 record added"; 
 mysql_close ($con) 
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.