PHP Update Data In MySQL

Good Day!!!

Yesterday, we create a tutorials for Inserting Data, Select Data, and Deleting Data from MySQL. For this follow-up tutorial, we are going to create another that related to them. We are going to create PHP Update Data In MySQL. For this tutorial, we are going to use the UPDATE Statement to update the current data in our table. This is the data in our table. In the underline, that we are going to update the first name. Data

Updating Data Using MySQLi and PDO

Using MySQLi (Object-Oriented)

  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "edit_query_pdo";
  6.  
  7. // Create connection
  8. $conn = new mysqli($servername, $username, $password, $dbname);
  9. // Check connection
  10. if ($conn->connect_error) {
  11. die("Connection failed: " . $conn->connect_error);
  12. }
  13.  
  14. $sql = "UPDATE tbl_registration SET first_name='Sarah' WHERE tbl_registration_id=2";
  15.  
  16. if ($conn->query($sql) === TRUE) {
  17. echo "Record updated successfully";
  18. } else {
  19. echo "Error updating record: " . $conn->error;
  20. }
  21.  
  22. $conn->close();
  23. ?>

Using MySQLi (Procedural)

  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "edit_query_pdo";
  6.  
  7. // Create connection
  8. $conn = mysqli_connect($servername, $username, $password, $dbname);
  9. // Check connection
  10. if (!$conn) {
  11. die("Connection failed: " . mysqli_connect_error());
  12. }
  13.  
  14. $sql = "UPDATE tbl_registration SET first_name='Sarah' WHERE tbl_registration_id=2";
  15.  
  16. if (mysqli_query($conn, $sql)) {
  17. echo "Record updated successfully";
  18. } else {
  19. echo "Error updating record: " . mysqli_error($conn);
  20. }
  21.  
  22. mysqli_close($conn);
  23. ?>

Using PDO (PHP Data Objects)

  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "edit_query_pdo";
  6.  
  7. try {
  8. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  9. // set the PDO error mode to exception
  10. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11.  
  12. $sql = "UPDATE tbl_registration SET first_name='Sarah' WHERE tbl_registration_id=2";
  13.  
  14. // Prepare statement
  15. $stmt = $conn->prepare($sql);
  16.  
  17. // execute the query
  18. $stmt->execute();
  19.  
  20. // echo a message to say the UPDATE succeeded
  21. echo $stmt->rowCount() . " records UPDATED successfully";
  22. }
  23. catch(PDOException $e)
  24. {
  25. echo $sql . "<br>" . $e->getMessage();
  26. }
  27.  
  28. $conn = null;
  29. ?>
And, this is another source code to update the record in the database table. In this source code, you can edit one or more column in our table.
  1. <?php
  2. include 'connection.php';
  3.  
  4. $get_id=$_REQUEST['tbl_registration_id'];
  5.  
  6. $first_name= $_POST['first_name'];
  7. $middle_name= $_POST['middle_name'];
  8. $last_name= $_POST['last_name'];
  9. $email= $_POST['email'];
  10. $contact_number= $_POST['contact_number'];
  11.  
  12. $sql = "UPDATE tbl_registration SET first_name ='$first_name', middle_name ='$middle_name',
  13. last_name ='$last_name', email ='$email', contact_number ='$contact_number' WHERE tbl_registration_id = '$get_id' ";
  14.  
  15. $conn->exec($sql);
  16. echo "<script>alert('Successfully Edit The Account!'); window.location='index.php'</script>";
  17. ?>

Output:

This is the Edit Form. Edit FormAfter the data updated, this is our new table. Result1Result Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Comments

Submitted bymartin kyabaggu (not verified)on Tue, 01/03/2017 - 18:06

The web based car hire sonko car company when i insert data, message error comes saying registration failed try again. any one understand the source of that error? please help me or someone who developed the system.

Add new comment