MySQL 表中使用 INSERT INTO SQL语句来插入数据。
你可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据。
以下为向MySQL数据表插入数据通用的 INSERT INTO SQL语法:
INSERT INTO table_name ( field1, field2,...fieldN ) |
VALUES |
( value1, value2,...valueN ); |
以下我们将使用 SQL INSERT INTO 语句向 MySQL 数据表 web3_tbl 插入数据
以下实例中我们将想 web3_tbl 表插入三条数据:
root@host# mysql -u root -p password; |
Enter password:******* |
mysql> use RUNOOB; |
Database changed |
mysql> INSERT INTO web3_tbl |
->(web3_title, web3_author, submission_date) |
->VALUES |
->("Learn PHP", "John Poul", NOW()); |
Query OK, 1 row affected (0.01 sec) |
mysql> INSERT INTO web3_tbl |
->(web3_title, web3_author, submission_date) |
->VALUES |
->("Learn MySQL", "Abdul S", NOW()); |
Query OK, 1 row affected (0.01 sec) |
mysql> INSERT INTO web3_tbl |
->(web3_title, web3_author, submission_date) |
->VALUES |
->("JAVA Tutorial", "Sanjay", '2007-05-06'); |
Query OK, 1 row affected (0.01 sec) |
mysql> |
在以上实例中,我们并没有提供 web3_id 的数据,因为该字段我们在创建表的时候已经设置它为 AUTO_INCREMENT(自动增加) 属性。 所以,该字段会自动递增而不需要我们去设置。实例中 NOW() 是一个 MySQL 函数,该函数返回日期和时间。
使用PHP脚本插入数据你可以使用PHP 的 mysql_query() 函数来执行 SQL INSERT INTO命令来插入数据。
该函数有两个参数,在执行成功时返回 TRUE,否则返回 FALSE。
bool mysql_query( sql, connection ); |
参数 | 描述 |
---|---|
sql | 必需。规定要发送的 SQL 查询。注释:查询字符串不应以分号结束。 |
connection | 可选。规定 SQL 连接标识符。如果未规定,则使用上一个打开的连接。 |
以下实例中程序接收用户输入的三个字段数据,并插入数据表中:
<html> |
<head> |
<meta charset="utf-8"> |
<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() ) |
{ |
$web3_title = addslashes ($_POST['web3_title']); |
$web3_author = addslashes ($_POST['web3_author']); |
} |
else |
{ |
$web3_title = $_POST['web3_title']; |
$web3_author = $_POST['web3_author']; |
} |
$submission_date = $_POST['submission_date']; |
$sql = "INSERT INTO web3_tbl ". |
"(web3_title,web3_author, submission_date) ". |
"VALUES ". |
"('$web3_title','$web3_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 ?>"> |
<table width="600" border="0" cellspacing="1" cellpadding="2"> |
<tr> |
<td width="250">Tutorial Title</td> |
<td> |
<input name="web3_title" type="text" id="web3_title"> |
</td> |
</tr> |
<tr> |
<td width="250">Tutorial Author</td> |
<td> |
<input name="web3_author" type="text" id="web3_author"> |
</td> |
</tr> |
<tr> |
<td width="250">Submission Date [ yyyy-mm-dd ]</td> |
<td> |
<input name="submission_date" type="text" id="submission_date"> |
</td> |
</tr> |
<tr> |
<td width="250"> </td> |
<td> </td> |
</tr> |
<tr> |
<td width="250"> </td> |
<td> |
<input name="add" type="submit" id="add" value="Add Tutorial"> |
</td> |
</tr> |
</table> |
</form> |
<?php |
} |
?> |
</body> |
</html> |
你也可以添加其他检查数据的方法,比如邮箱格式验证,电话号码验证,是否为整数验证等。