Home >Backend Development >PHP Tutorial >Updating Data into MySQL Database

Updating Data into MySQL Database

PHP中文网
PHP中文网forward
2024-12-05 10:29:16676browse

Data can be updated into MySQL tables by executing SQL UPDATE statement through PHP function mysql_query.

Below is a simple example to update records into employee table. To update a record in any table it is required to locate that record by using a conditional clause. Below example uses primary key to match a record in employee table.

截屏2024-12-05 10.15.12.png

Example

Try out following example to understand update operation. You need to provide an employee ID to update an employee salary.

   
   
      <title>Update a Record in MySQL Database</title>
   
   
   
      <?php          if(isset($_POST[&#39;update&#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());
            }
            
            $emp_id = $_POST[&#39;emp_id&#39;];
            $emp_salary = $_POST[&#39;emp_salary&#39;];
            
            $sql = "UPDATE employee ". "SET emp_salary = $emp_salary ". 
               "WHERE emp_id = $emp_id" ;
            mysql_select_db(&#39;test_db&#39;);
            $retval = mysql_query( $sql, $conn );
            
            if(! $retval ) {
               die(&#39;Could not update data: &#39; . mysql_error());
            }
            echo "Updated data successfullyn";
            
            mysql_close($conn);
         }else {
            ?>
               
">                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Employee ID
Employee Salary
  
                                                      
               
                       


The above is the detailed content of Updating Data into MySQL Database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete