PHP – Inserting the Data to the Database

By | June 30, 2020
PHP - Inserting the Data to the Database

Inserting data to the database is the same process as creating or deleting the database. You can use the same function mysql_query to add the data to the table.

Example-

Vamware
<?php
   $db_host = 'localhost:3036';
   $db_user = 'root';
   $db_pass = 'rootpassword';
   $db_conn = mysql_connect($db_host, $db_user, $db_pass);
   if(! $db_conn ) {
      die('Connection failed: ' . mysql_error());
   }
   echo 'Success';
   $db_sql = ‘insert into student’. 
‘(stu_id, stu_name, stu_address)’.
‘Values (1, “Jacob”,” Delhi”)’;
   mysql_select_db('demo_db');
   $db_retval = mysql_query( $db_sql, $conn );
   if(! $db_retval ) {
      die('data insertion failed: ' . mysql_error());
   }
   echo "Table data is inserted successfully\n";
   mysql_close($db_conn);
?>

Adding table data using HTML form

There is another process that is normally used to insert the data within the table present in the database. The data is usually inserted through an HTML form from the front end and directly get inserted to the database.

Example-

<html>
   <head>
      <title>Adding New Record in Demo table</title>
   </head>
   <body>
      <?php
         if(isset($_POST['add'])) {
 $db_host = 'localhost:3036';
   $db_user = 'root';
   $db_pass = 'rootpassword';
   $db_conn = mysql_connect($db_host, $db_user, $db_pass);
   if(! $db_conn ) {
      die('Connection failed: ' . mysql_error());
   }
if(! get_magic_quotes_gpc() ) {
               $stu_name = addslashes ($_POST['stu_name']);
               $stu_address = addslashes ($_POST['stu_address']);
            }else {
               $stu_name = $_POST['stu_name'];
               $stu_address = $_POST['stu_address'];
            }
 $db_sql = ‘insert into student’. 
‘( stu_name, stu_address)’.
‘Values (‘$stu_name’, ‘$stu_address’)”;
 mysql_select_db('demo_db');
   $db_retval = mysql_query( $db_sql, $conn );   
   if(! $db_retval ) {
      die('data insertion failed: ' . mysql_error());
   }
   echo "Table data is inserted successfully\n";
   mysql_close($db_conn);
}
else {
            ?>
               <form method = "post" action = "<?php $_PHP_SELF ?>">
                  <table width = "400" border = "0" cellspacing = "1" 
                     cellpadding = "2">                  
                     <tr>
                        <td width = "100">Student Name</td>
                        <td><input name = "stu_name" type = "text" 
                           id = "stu_name"></td>
                     </tr>                  
                     <tr>
                        <td width = "100">Student Address</td>
                        <td><input name = "stu_address" type = "text" 
                           id = "stu_address"></td>
                     </tr>
<tr>
                        <td width = "100"> </td>
                        <td> </td>
                     </tr>
                     <tr>
                        <td width = "100"> </td>
                        <td>
                           <input name = "add" type = "submit" id = "add" 
                              value = "Add Student">
                        </td>
                     </tr>                  
                  </table>
               </form>           
            <?php
         }
      ?>   
   </body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *