要想在 MySQL 表中插入數(shù)據(jù),需要使用 INSERT INTO 這個(gè) SQL 命令。既可以使用mysql> 提示符方式,也可以使用 PHP 等腳本來(lái)完成。
利用 INSERT INTO 命令為表插入數(shù)據(jù)的一般語(yǔ)法如下所示:
INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );
要想插入字符串類型的數(shù)據(jù),需要把值用雙引號(hào)或單引號(hào)包括起來(lái),比如:"value"。
我們將使用 INSERT INTO 命令為表 tutorials_tbl 插入數(shù)據(jù)。
在下面的范例中,我們將為表 tutorials_tbl 創(chuàng)建3條記錄。
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> INSERT INTO tutorials_tbl
->(tutorial_title, tutorial_author, submission_date)
->VALUES
->("Learn PHP", "John Poul", NOW());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO tutorials_tbl
->(tutorial_title, tutorial_author, submission_date)
->VALUES
->("Learn MySQL", "Abdul S", NOW());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO tutorials_tbl
->(tutorial_title, tutorial_author, submission_date)
->VALUES
->("JAVA Tutorial", "Sanjay", '2007-05-06');
Query OK, 1 row affected (0.01 sec)
mysql>
注意:代碼中的箭頭符號(hào)(
->)不屬于 SQL 命令。它們只是用來(lái)表示換行,如果我們?cè)诿啃忻钅┪膊惶砑臃痔?hào)(;),按下回車鍵時(shí),MySQL 命令提示符就會(huì)自動(dòng)創(chuàng)建出這個(gè)符號(hào)。
在上面的范例中,我們沒有提供 tutorial_id,因?yàn)樵趧?chuàng)建表時(shí),已為該字段提供了 AUTO_INCREMENT 選項(xiàng)。因此 MySQL 會(huì)自動(dòng)知道插入 ID。NOW() 是一個(gè)能夠返回當(dāng)前日期與時(shí)間的 MySQL 函數(shù)。
同樣,也可以在 PHP 的 mysql_query()函數(shù)中使用 SQL 命令 INSERT INTO 為 MySQL 表插入數(shù)據(jù)。
在下面這個(gè)范例中,從用戶那里接收3個(gè)參數(shù),然后將這些參數(shù)插入到 MySQL 表中。
<html>
<head>
<title>Add New Record in MySQL Database</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() )
{
$tutorial_title = addslashes ($_POST['tutorial_title']);
$tutorial_author = addslashes ($_POST['tutorial_author']);
}
else
{
$tutorial_title = $_POST['tutorial_title'];
$tutorial_author = $_POST['tutorial_author'];
}
$submission_date = $_POST['submission_date'];
$sql = "INSERT INTO tutorials_tbl ".
"(tutorial_title,tutorial_author, submission_date) ".
"VALUES ".
"('$tutorial_title','$tutorial_author','$submission_date')";
mysql_select_db('TUTORIALS');
$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="tutorial_title" type="text" id="tutorial_title">
</td>
</tr>
<tr>
<td width="250">Tutorial Author</td>
<td>
<input name="tutorial_author" type="text" id="tutorial_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>
在輸入數(shù)據(jù)時(shí),使用函數(shù) get_magic_quotes_gpc() 檢查當(dāng)前是否配置了魔術(shù)引號(hào)。如果函數(shù)返回 false,則使用 addslashes() 在引號(hào)前添加反斜杠。
另外,還可以針對(duì)輸入數(shù)據(jù)進(jìn)行多種驗(yàn)證,確保數(shù)據(jù)的合法性,并對(duì)其采取正確行為。