- MySQLi 教程
- MySQLi - 首頁
- MySQLi - 簡介
- MySQLi - PHP 語法
- MySQLi - 連線
- MySQLi - 建立資料庫
- MySQLi - 刪除資料庫
- MySQLi - 選擇資料庫
- MySQLi - 建立表
- MySQLi - 刪除表
- MySQLi - 插入查詢
- MySQLi - 選擇查詢
- MySQLi - WHERE 子句
- MySQLi - 更新查詢
- MySQLi - 刪除查詢
- MySQLi - LIKE 子句
- MySQLi - 排序結果
- MySQLi - 使用連線
- MySQLi - 處理 NULL 值
- 獲取和使用 MySQLi 元資料
- MySQL
- MySQL - 安裝
- MySQL - 管理
- MySQL - 資料型別
- MySQL - 正則表示式
- MySQL - 事務
- MySQL - ALTER 命令
- MySQL - 索引
- MySQL - 臨時表
- MySQL - 克隆表
- MySQL - 使用序列
- MySQL - 處理重複資料
- MySQLi 有用資源
- MySQLi - 有用函式
- MySQLi - 快速指南
- MySQLi - 有用資源
- MySQLi - 討論
MySQLi - 插入查詢
要將資料插入 MySQL 表中,您需要使用 SQL 的 **INSERT INTO** 命令。您可以使用 mysql> 提示符或使用任何指令碼(如 PHP)將資料插入 MySQL 表中。
語法
以下是將資料插入 MySQL 表的 INSERT INTO 命令的通用 SQL 語法:
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN );
要插入字串資料型別,需要將所有值放在雙引號或單引號中。例如 **"value"**。
從命令提示符插入資料
要從命令提示符插入資料,我們將使用 SQL INSERT INTO 命令將資料插入 MySQL 表 tutorials_tbl。
示例
以下示例將在 **tutorials_tbl** 表中建立 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>
**注意** - 請注意,所有箭頭符號 (→) 都不屬於 SQL 命令的一部分。它們表示新行,並且在按 Enter 鍵而不給出每個命令列末尾的分號時,由 MySQL 提示符自動建立。
在上面的示例中,我們沒有提供 tutorial_id,因為在建立表時,我們為此欄位提供了 AUTO_INCREMENT 選項。因此,MySQL 會自動處理插入這些 ID。這裡,**NOW()** 是一個 MySQL 函式,它返回當前日期和時間。
使用 PHP 指令碼插入資料
PHP 使用 **mysqli_query()** 或 **mysql_query()** 函式將記錄插入 MySQL 表中。此函式需要兩個引數,並在成功時返回 TRUE,失敗時返回 FALSE。
語法
$mysqli→query($sql,$resultmode)
| 序號 | 引數及描述 |
|---|---|
| 1 |
$sql 必需 - 將記錄插入表的 SQL 查詢。 |
| 2 |
$resultmode 可選 - 根據所需的行為,使用常量 MYSQLI_USE_RESULT 或 MYSQLI_STORE_RESULT。預設情況下,使用 MYSQLI_STORE_RESULT。 |
示例
此示例將從使用者處獲取三個引數,並將它們插入 MySQL 表中:
複製並貼上以下示例作為 mysql_example.php:
<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['add'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root@123';
$dbname = 'TUTORIALS';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($mysqli→connect_errno ) {
printf("Connect failed: %s<br />", $mysqli→connect_error);
exit();
}
printf('Connected successfully.<br />');
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')";
if ($mysqli→query($sql)) {
printf("Record inserted successfully.<br />");
}
if ($mysqli→errno) {
printf("Could not insert record into table: %s<br />", $mysqli→error);
}
$mysqli→close();
} 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>
輸出
訪問部署在 apache web 伺服器上的 mysql_example.php,輸入詳細資訊並在提交表單後驗證輸出。
Record inserted successfully.
在執行資料插入時,最好使用函式 **get_magic_quotes_gpc()** 檢查是否設定了 magic quote 的當前配置。如果此函式返回 false,則使用函式 **addslashes()** 在引號前新增反斜槓。
您可以圍繞它進行許多驗證,以檢查輸入的資料是否正確,並可以採取適當的操作。