Home >Backend Development >PHP Tutorial >Updating Data into MySQL Database
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.
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['update'])) { $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $emp_id = $_POST['emp_id']; $emp_salary = $_POST['emp_salary']; $sql = "UPDATE employee ". "SET emp_salary = $emp_salary ". "WHERE emp_id = $emp_id" ; mysql_select_db('test_db'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not update data: ' . mysql_error()); } echo "Updated data successfullyn"; mysql_close($conn); }else { ?>
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!