UPDATE Statement is used to update the records of MySQL table. Some changes are made to the rows given by the update statement.
UPDATE table_name SET column 1 = newvalue 1, column 2 = newvalue 2, ..., column n = newvalue n WHERE column = value
The program uses SET and WHERE clause with UPDATE statement. After the WHERE clause, which record to update is given the column_name and its existing_value.
When the WHERE clause is not used, the values of the columns given with ‘SET’ are updated on the records of the entire columns.
Updating Record using MySQLi
<?php $server = "localhost"; $user = "root"; $password = ""; $db = "expertstutorials"; $conn = mysqli_connect($server, $user, $password, $db); if($conn){ echo "Connected successfully."; } else{ echo "Connection failed : ".mysqli_connect_error(); } $table_update = "UPDATE course SET course_name = 'm.tech', course_year = '2 year' WHERE id = 1"; if (mysqli_query($conn, $table_update)) { echo "Table Updated successfully."; } else { echo "Table Updating failed : " . mysqli_error($conn); } mysqli_close($conn); ?>
Updating Record using PDO
<?php $server = "localhost"; $user = "root"; $password = ""; $db = "expertstutorials"; try{ $conn = new PDO("mysql:host=$server;dbname=$db", $user, $password); echo "Connected successfully."; $table_update = "UPDATE course SET course_name = 'm.tech', course_year = '2 year' WHERE id = 1"; $conn->exec($table_update); echo "Table Updated successfully."; } catch(PDOException $e){ echo "Table updating failed : " . $e->getMessage(); } $conn = null; ?>