首頁  >  文章  >  後端開發  >  我們如何使用PHP腳本將資料插入到現有的MySQL表中?

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

王林
王林轉載
2023-09-13 17:29:021584瀏覽

我們如何使用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刪除