Online training and video tutorials
Enter     Registration
     Contacts

 
     
 
Max Azimov
St. Petersburg

Articles [97]

User's video [259]





PHP Update Data in MySQL
 
Update Data In a MySQL Table Using MySQLi and PDO
 
The UPDATE statement is used to update existing records in a table:
  1. UPDATE table_name
  2. SET column1=value, column2=value2,...
  3. WHERE some_column=some_value 
Notice the WHERE clause in the UPDATE syntax: The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
 
To learn more about SQL, please visit our SQL tutorial.
 
Let's look at the "MyGuests" table:
 
id firstname lastname email reg_date
1 John Doe john@example.com 2014-10-22 14:26:15
2 Mary Moe mary@example.com 2014-10-23 10:22:30
 
The following examples update the record with id=2 in the "MyGuests" table:
 
Example (MySQLi Object-oriented)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if ($conn->connect_error) {
  10.     die("Connection failed: " . $conn->connect_error);
  11. $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
  12. if ($conn->query($sql) === TRUE) {
  13.     echo "Record updated successfully";
  14. } else {
  15.     echo "Error updating record: " . $conn->error;
  16. }
  17. $conn->close();
  18. ?>
 
Example (MySQLi Procedural)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = mysqli_connect($servername, $username, $password, $dbname);
  8. // Check connection
  9. if (!$conn) {
  10.     die("Connection failed: " . mysqli_connect_error());
  11. }
  12. $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
  13. if (mysqli_query($conn, $sql)) {
  14.     echo "Record updated successfully";
  15. } else {
  16.     echo "Error updating record: " . mysqli_error($conn);
  17. }
  18. mysqli_close($conn);
  19. ?>
  20.  
Example (PDO)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDBPDO";
  6. try {
  7.     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  8.     // set the PDO error mode to exception
  9.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.     $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
  11.     // Prepare statement
  12.     $stmt = $conn->prepare($sql);
  13.     // execute the query
  14.     $stmt->execute();
  15.     // echo a message to say the UPDATE succeeded
  16.     echo $stmt->rowCount() . " records UPDATED successfully";
  17.     }
  18. catch(PDOException $e)
  19.     {
  20.     echo $sql . "<br>" . $e->getMessage();
  21.     }
  22. $conn = null;
  23. ?>
 
After the record is updated, the table will look like this:
id firstname lastname email reg_date
1 John Doe john@example.com 2014-10-22 14:26:15
2 Mary Doe mary@example.com 2014-10-23 10:22:30

 



2016-12-10
453 views




 
     
 
Map      Distance education, online learning courses. Project PLANETSTUDY - distance learning online.
© 2012 PlanetStudy

Catalog.299.ru

The site is a media provider
Яндекс.Метрика
 
English USA
Australia Русский