首页  >  文章  >  后端开发  >  我们如何使用PHP脚本将数据插入到现有的MySQL表中?

我们如何使用PHP脚本将数据插入到现有的MySQL表中?

王林
王林转载
2023-09-13 17:29:021528浏览

我们如何使用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删除