MySQL

How to update data in MySQL database using PHP PDO

In this tutorial, we are going to see how to update data in MySQL database using PHP PDO. If you are a “newbie” you should know how to connect to a MySQL database before using the code below. You cannot update data in a table if you are not connected to it.
 

 

How to update data in MySQL database using PHP PDO

In the example below, we are going to update the column ‘name’, ‘age’ and ‘address’ from users table using PHP PDO.

$sql = "UPDATE users SET name = :name, 
          age = :age, 
          address = :address  
          WHERE id = :id";
		  
$stmt = $pdo->prepare($sql);                                  
$stmt->bindParam(':name', $_POST['name'], PDO::PARAM_STR);     
$stmt->bindParam(':age', $_POST['$age'], PDO::PARAM_INT);    
$stmt->bindParam(':address', $_POST['address'], PDO::PARAM_STR);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);   
$stmt->execute();
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *