How can we insert data into existing MySQL table using PHP script?

王林
Release: 2023-09-13 17:30:01
forward
1555 people have browsed it

How can we insert data into existing MySQL table using PHP script?

As we all know, PHP provides us with a function called mysql_query for inserting data into an existing MySQL table.

Example

To illustrate this, we insert data into a table named "Tutorials_tbl" with the help of PHP script in the following example -

<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>
Copy after login

The above is the detailed content of How can we insert data into existing MySQL table using PHP script?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!