我們如何使用PHP腳本將資料插入到現有的MySQL表中?

王林
發布: 2023-09-13 17:30:01
轉載
1556 人瀏覽過

我們如何使用PHP腳本將資料插入到現有的MySQL表中?

眾所周知,PHP 為我們提供了名為 mysql_query 的函數,用於將資料插入現有的 MySQL 表中。

範例

到為了說明這一點,我們在以下範例中使用 PHP 腳本將資料插入名為「Tutorials_tbl」的表中 -

<html>
   <head>
      <title>Add New Record in MySQL Database</title>
   </head>

   <body>
      <?php
         if(isset($_POST[&#39;add&#39;])) {
            $dbhost = &#39;localhost:3036&#39;;
            $dbuser = &#39;root&#39;;
            $dbpass = &#39;rootpassword&#39;;
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);
           
            if(! $conn ) {
               die(&#39;Could not connect: &#39; . mysql_error());
            }
           
            if(! get_magic_quotes_gpc() ) {
               $tutorial_title = addslashes ($_POST[&#39;tutorial_title&#39;]);
               $tutorial_author = addslashes
               ($_POST[&#39;tutorial_author&#39;]);
            } else {
               $tutorial_title = $_POST[&#39;tutorial_title&#39;];
               $tutorial_author = $_POST[&#39;tutorial_author&#39;];
            }
           
            $submission_date = $_POST[&#39;submission_date&#39;];
            $sql = "INSERT INTO tutorials_tbl ".
               "(tutorial_title,tutorial_author, submission_date)
               "."VALUES ".
               "(&#39;$tutorial_title&#39;,&#39;$tutorial_author&#39;,&#39;$submission_date&#39;)";
           
            mysql_select_db(&#39;TUTORIALS&#39;);
            $retval = mysql_query( $sql, $conn );
            if(! $retval ) {
               die(&#39;Could not enter data: &#39; . mysql_error());
            }
            echo "Entered data successfully</p><p>";
            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>
登入後複製

以上是我們如何使用PHP腳本將資料插入到現有的MySQL表中?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!