&"/> AddNewRecordinMySQLDatabase &">

Maison  >  Article  >  développement back-end  >  Comment pouvons-nous insérer des données dans une table MySQL existante à l'aide d'un script PHP ?

Comment pouvons-nous insérer des données dans une table MySQL existante à l'aide d'un script PHP ?

王林
王林avant
2023-09-13 17:29:021528parcourir

Comment pouvons-nous insérer des données dans une table MySQL existante à laide dun script PHP ?

Comme nous le savons tous, PHP nous fournit une fonction appelée mysql_query pour insérer des données dans une table MySQL existante.

Exemple

Pour illustrer cela, nous insérons des données dans une table nommée « Tutorials_tbl » à l'aide du script PHP dans l'exemple suivant -

<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>

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer