Updating Data within the Database Using PHP

By | June 30, 2020
Updating Data within the Database Using PHP

If you want to update any record within the table of the database then you can make use of the mysql_query function and use the update command. 

You can use any query using the mysql_query function and solve your purpose.

Vamware

Example:

  <?php
         if(isset($_POST['update'])) {
  $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());
   }
$stu_id = $_POST['stu_id'];
            $stu_address = $_POST['emp_address'];
$db_sql = "UPDATE student ". "SET stu_address = $stu_address ". 
               "WHERE stu_id = $stu_id" ;
   mysql_select_db('demo_db');
   $db_retval = mysql_query( $db_sql, $db_conn );
if(! $db_retval ) {
      die('data cannot not be updated: ' . mysql_error());
   }
echo "updated data 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 ID</td>
                        <td><input name = "stu_id" type = "text" 
                           id = "stu_id"></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 = "update" type = "submit" 
                              id = "update" value = "Update">
                        </td>
                     </tr>
                  </table>
               </form>
            <?php
         }
      ?>
   </body>
</html>

Leave a Reply

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