PHP SQL Update

PHP SQL Update is used to execute the mysql _update () function that modify the table structure and set the new value for the existing value on the condition specified in Where Clause.

PHP SQL Update

PHP SQL Update

     

PHP SQL Update is used to execute the mysql _update () function that modify the table structure and set the new value for the existing value on the condition specified in Where Clause.

Understand with Example

 This example illustrates an example from 'PHP SQL Update'. To understand and grasp the example we create a update query "UPDATE emp_table SET emp_name = 'amar' WHERE emp_name = 'sandeep' AND emp_designation = 'programmer'". The update table is used to modify any field value of any MySQL table in PHP.

Syntax: The SQL syntax of UPDATE command is used to perform update data into MySQL table:

 

 

UPDATE table_name SET field1=new-value1, field2=new-value2[WHERE Clause]
  • The UPDATE query is used to modify one or more field all together.

  • The WHERE clause is used to specify any condition.

  • You can update values in a single table at a time.

The WHERE clause is very useful when you want to update selected rows in a table.

 

Table: emp_table

 

Table: emp_table

 

Source Code of sql_update.php 

<?php
  $con = mysql_connect("localhost","root","root");
  if (!$con){
  die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("test", $con);

  mysql_query("UPDATE emp_table SET emp_name = 'amar' WHERE emp_name 

  = 'sandeep' AND emp_designation = 'programmer'");

  echo "Column emp_name updated successfully<br><br>";

  $result = mysql_query("SELECT * FROM emp_table");
  echo "<table border='1'>
  <tr>
  <th>ID</th>
  <th>Name</th>
  <th>Designation</th>
  </tr>";
  while ($row = mysql_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['emp_id'] "</td>";
  echo "<td>" . $row['emp_name'] "</td>";
  echo "<td>" . $row['emp_designation'] "</td>";
  echo "</tr>";
  }
  echo "</table>";

  mysql_close($con);
?>

Download Source Code

Output: