INSERT INTO SQL statement is used to insert data into MySQL table .
Data can be inserted into the data table through the mysql> command prompt window, or through a PHP script.
The following is the general INSERT INTO SQL syntax for inserting data into a MySQL data table :
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1 , value2 , ... valueN );
If the data is of character type, single or double quotes must be used, such as "value".
Insert data via command prompt window
The following will use the SQL INSERT INTO statement to insert data into the MySQL data table runoob_tbl
The following example will insert three pieces of data into the runoob_tbl table:
root@host# mysql -u root -p password; Enter password:******* mysql>use RUNOOB;Database changed mysql> INSERT INTO runoob_tbl (runoob_title, runoob_author, submission_date) VALUES("Learn PHP","John Poul", NOW()); mysql> INSERT INTO runoob_tbl (runoob_title, runoob_author, submission_date) VALUES ("Learn MySQL","Abdul S", NOW()); mysql> INSERT INTO runoob_tbl (runoob_title, runoob_author, submission_date) VALUES ("JAVA Tutorial","Sanjay",'2007-05-06'); mysql>
In the above example, the data of runoob_id is not provided, because the field has been set as the AUTO_INCREMENT attribute when the table is created. So, this field will be incremented automatically without needing to be set. In the example NOW() is a MySQL function that returns a date and time.
Insert data using PHP script
You can use PHP's mysql_query() function to execute the SQL INSERT INTO command to insert data.
The function has two parameters and returns true if the execution is successful, otherwise it returns false.
bool mysql_query( sql, connection );
sql | Required. Specifies the SQL query to send. NOTE: The query string should not end with a semicolon. |
connection | Optional. Specifies the SQL connection identifier. If not specified, the last open connection is used. |
Example
The program in the following example receives three fields of data entered by the user and inserts them into the data table:
<html>
<head>
<metacharset="gb2312">
<title>向 MySQL 数据库添加数据</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$dbhost ='localhost:3036';
$dbuser ='root';
$dbpass ='rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: '. mysql_error());
}
if(! get_magic_quotes_gpc())
{
$runoob_title = addslashes ($_POST['runoob_title']);
$runoob_author = addslashes ($_POST['runoob_author']);
}
else
{
$runoob_title = $_POST['runoob_title'];
$runoob_author = $_POST['runoob_author'];
}
$submission_date = $_POST['submission_date'];
$sql ="INSERT INTO runoob_tbl ".
"(runoob_title,runoob_author, submission_date) ".
"VALUES ".
"('$runoob_title','$runoob_author','$submission_date')";
mysql_select_db('RUNOOB');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: '. mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<tablewidth="600"border="0"cellspacing="1"cellpadding="2">
<tr>
<tdwidth="250">Tutorial Title</td>
<td>
<inputname="runoob_title"type="text"id="runoob_title">
</td>
</tr>
<tr>
<tdwidth="250">Tutorial Author</td>
<td>
<inputname="runoob_author"type="text"id="runoob_author">
</td>
</tr>
<tr>
<tdwidth="250">Submission Date [ yyyy-mm-dd ]</td>
<td>
<inputname="submission_date"type="text"id="submission_date">
</td>
</tr>
<tr>
<tdwidth="250"></td>
<td></td>
</tr>
<tr>
<tdwidth="250"></td>
<td>
<inputname="add"type="submit"id="add"value="Add Tutorial">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>