saftutorials.com
MYSQL UPDATE DATA

Create the MySQL query for updating data into a table and then execute it with mysql_query() function. We can update data of a table by using MYSQL UPDATE statement.


Syntax of MYSQL UPDATE statement

UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value


Below is a simple example to update records into student table

<html>
 <head>
  <title>Update a Record in MySQL Database</title>
 </head>
 <body>
    <?php
    if(isset($_POST['update'])) {
    $database_host = 'localhost:3036';
    $database_user = 'root';
    $database_pass = '';
    $conn = mysql_connect($database_host, $database_user, $database_pass);
 
    if(! $conn ) {
      die('Could not connect: ' . mysql_error());
    }else{
      echo 'Connected successfully\n';
    }
  
    $selected_or_not = mysql_select_db( 'xyz_school' );
    if(! $selected_or_not ) {
      die('Not Selected xyz_school database');
    }else{
      echo 'Selected xyz_school database';
    }
        
        $student_id = $_POST['student_id'];
        $student_name = $_POST['student_name']
        $student_address = $_POST['student_address'];
        
        $sql = "UPDATE student SET student_name = $student_name, student_address= $student_address WHERE student_id = $student_id" ;
        
        $retval = mysql_query( $sql, $conn );
        
        if(! $retval ) {
           die('Could not update data: ' . mysql_error());
        }
        echo "Updated data successfully\n";
        
        mysql_close($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="student_id" type="text" id="student_id"></td>
</tr>
 
<tr>
<td width="100">Student Name</td>
<td><input name="student_name" type="text" id="student_name"></td>
</tr>
 
<tr>
<td width="100">Enrollment No </td>
<td><input name="enrollment_no" type="text" id="enrollment_no"> </td>
</tr>
 
<tr>
<td width="100">Address </td>
<td><textarea name="student_address" id="student_address"></textarea></td>
</tr>
 
<tr>
<td width="100"> </td>
<td><input name="update" type="submit" id="update" value="Update"></td>
</tr>
 
 </table>
   </form>
    <?php
     }
    ?>
 </body>
</html>

Demo Update Form where update and Student information by using his Unique Student ID
Student ID
Student Name
Enrollment No
Address


Multiple Data Update

If you want to Update multiple data in one Query like Update all Student Semester from on Query then you should only one change for this:

$sql = "UPDATE student SET student_name = $student_name, student_address= $student_address WHERE semester or column_name = 'with_you_value' " ;





Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic