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

Updating Data into MySQL Database

PHP中文网
Release: 2024-12-05 10:29:16
forward
696 people have browsed it

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 {
            ?>
               
Copy after login
">                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template