在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)庫/ MySQL 插入查詢
MySQL 復(fù)制表
MySQL ALTER 命令
MySQL 安裝
MySQL 日期與時間方面的函數(shù)
MySQL SQL Injection
MySQL 排序結(jié)果
MySQL 臨時表
MySQL 介紹
MySQL 數(shù)據(jù)導(dǎo)出
MySQL 索引
MySQL 數(shù)值函數(shù)
MySQL 更新查詢
MySQL UNION 關(guān)鍵字
MySQL RAND 函數(shù)
創(chuàng)建 MySQL 數(shù)據(jù)庫
MySQL AVG 函數(shù)
MySQL Using Join
MySQL Handling Duplicates
MySQL SUM 函數(shù)
MySQL 數(shù)據(jù)類型
MySQL 插入查詢
MySQL 字符串函數(shù)
MySQL Using Sequences
MySQL 管理
MySQL 數(shù)據(jù)導(dǎo)入
MySQL BETWEEN 子句
MySQL MIN 函數(shù)
創(chuàng)建 MySQL 表
MySQL Group By 子句
MySQL COUNT 函數(shù)
MySQL 匯報
MySQL 選擇數(shù)據(jù)庫
MySQL Where Clause
MySQL 選擇查詢
MySQL Like Clause
MySQL 正則表達式
一些非常有用的學(xué)習(xí)資源
MySQL NULL Values
MySQL 刪除查詢
MySQL 數(shù)據(jù)庫信息
一些有用的 MySQL 函數(shù)與子句
MySQL 刪除表
MySQL MAX 函數(shù)
MySQL SQRT 函數(shù)
MySQL 終止數(shù)據(jù)庫
連接 MySQL 服務(wù)器
MySQL IN 子句
MySQL CONCAT 函數(shù)
MySQL PHP語法

MySQL 插入查詢

要想在 MySQL 表中插入數(shù)據(jù),需要使用 INSERT INTO 這個 SQL 命令。既可以使用mysql> 提示符方式,也可以使用 PHP 等腳本來完成。

語法格式

利用 INSERT INTO 命令為表插入數(shù)據(jù)的一般語法如下所示:

INSERT INTO table_name ( field1, field2,...fieldN )
                       VALUES
                       ( value1, value2,...valueN );

要想插入字符串類型的數(shù)據(jù),需要把值用雙引號或單引號包括起來,比如:"value"

從命令提示符中插入數(shù)據(jù)

我們將使用 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>

注意:代碼中的箭頭符號(->)不屬于 SQL 命令。它們只是用來表示換行,如果我們在每行命令末尾不添加分號(;),按下回車鍵時,MySQL 命令提示符就會自動創(chuàng)建出這個符號。

在上面的范例中,我們沒有提供 tutorial_id,因為在創(chuàng)建表時,已為該字段提供了 AUTO_INCREMENT 選項。因此 MySQL 會自動知道插入 ID。NOW() 是一個能夠返回當前日期與時間的 MySQL 函數(shù)。

利用 PHP 腳本插入數(shù)據(jù)

同樣,也可以在 PHP 的 mysql_query()函數(shù)中使用 SQL 命令 INSERT INTO 為 MySQL 表插入數(shù)據(jù)。

范例

在下面這個范例中,從用戶那里接收3個參數(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ù) get_magic_quotes_gpc() 檢查當前是否配置了魔術(shù)引號。如果函數(shù)返回 false,則使用 addslashes() 在引號前添加反斜杠。

另外,還可以針對輸入數(shù)據(jù)進行多種驗證,確保數(shù)據(jù)的合法性,并對其采取正確行為。