PHP MySQL Update Data (or) Record
PHP mysql_query() function is used to update record in a table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.
- mysqli_query()
- PDO::__query()
PHP MySQLi Update Record Example
Example
- <?php
- $host = 'localhost';
- $user = 'root';
- $pass = '';
- $dbname = 'test';
-
- $conn = mysqli_connect($host, $user, $pass,$dbname);
- if(!$conn){
- die('Could not connect: '.mysqli_connect_error());
- }
- echo 'Connected successfully<br/>';
-
- $id=4;
- $name="sam";
- $salary=80000;
- $sql = "update staff set name=\"$name\", salary=$salary where id=$id";
- if(mysqli_query($conn, $sql)){
- echo "Record updated successfully";
- }else{
- echo "Could not update record: ". mysqli_error($conn);
- }
-
- mysqli_close($conn);
- ?>
Output:
Connected successfully
Record updated successfully