Update guests examples (PDO)

0 records UPDATED successfully

Here is the code for deleting the database record.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  // Update the selected record with a new last name
  $sql = "UPDATE MyGuests SET lastname='' WHERE id=7";

  // Prepare statement
  $stmt = $conn->prepare($sql);

  // execute the query
  $stmt->execute();

  // echo a message to say the UPDATE succeeded
  echo "".$stmt->rowCount() . " records UPDATED successfully";
} catch(PDOException $e) {
  echo "".$sql . "<br>" . $e->getMessage()."";
}

$conn = null;
?>

This is what the database table looks like after the record is deleted.

IdFirstnameLastnameEmailRegistration Date
1JohnDoejohn@example.com2024-11-28 13:31:22
2JohnDeerejohn@deere.net2024-11-28 13:38:46
4DaisyDuckdaisyduck@example.com2024-11-28 13:46:18
5Plutopluto@example.com2024-11-28 13:46:18
6DonaldDuckdduck@example.com2024-12-05 06:47:46
7Daisydaisyduck@example.com2024-12-05 06:48:54
8Plutopluto@example.com2024-12-05 06:47:46
9JohnDeerejohn@deere.net2024-12-05 06:54:14
10JohnDoejohn@example.com2024-12-05 06:54:18
11DonaldDuckdduck@example.com2024-12-07 02:11:50
12DaisyDuckdaisyduck@example.com2024-12-07 02:11:50
13Plutopluto@example.com2024-12-07 02:11:50
14JohnDeerejohn@deere.net2024-12-07 02:13:49
15JohnDoejohn@example.com2024-12-08 00:09:04
16JohnDeerejohn@deere.net2024-12-08 03:13:10
17DonaldDuckdduck@example.com2024-12-08 03:39:28
18DaisyDuckdaisyduck@example.com2024-12-08 03:39:28
19Plutopluto@example.com2024-12-08 03:39:28
20DonaldDuckdduck@example.com2024-12-13 22:35:38
21DaisyDuckdaisyduck@example.com2024-12-13 22:35:38
22Plutopluto@example.com2024-12-13 22:35:38
23JohnDoejohn@example.com2024-12-13 22:37:14
24JohnDeerejohn@deere.net2024-12-13 22:42:26
25JohnDeerejohn@deere.net2024-12-20 01:25:39
26JohnDeerejohn@deere.net2024-12-22 19:07:39
27JohnDoejohn@example.com2024-12-22 19:08:16
28DonaldDuckdduck@example.com2024-12-22 19:21:13
29DaisyDuckdaisyduck@example.com2024-12-22 19:21:13
30Plutopluto@example.com2024-12-22 19:21:13
31JohnDeerejohn@deere.net2024-12-28 09:01:18
32JohnDoejohn@example.com2024-12-30 17:05:24
33DonaldDuckdduck@example.com2024-12-30 17:07:26
34DaisyDuckdaisyduck@example.com2024-12-30 17:07:26
35Plutopluto@example.com2024-12-30 17:07:26
36JohnDeerejohn@deere.net2025-01-03 21:34:45
37JohnDoejohn@example.com2025-01-03 21:41:36
38DonaldDuckdduck@example.com2025-01-03 21:53:00
39DaisyDuckdaisyduck@example.com2025-01-03 21:53:00
40Plutopluto@example.com2025-01-03 21:53:00
41JohnDoejohn@example.com2025-01-03 23:02:47
42DonaldDuckdduck@example.com2025-01-03 23:08:58
43DaisyDuckdaisyduck@example.com2025-01-03 23:08:58
44Plutopluto@example.com2025-01-03 23:08:58
45DonaldDuckdduck@example.com2025-01-04 15:05:58
46DaisyDuckdaisyduck@example.com2025-01-04 15:05:58
47Plutopluto@example.com2025-01-04 15:05:58
48JohnDeerejohn@deere.net2025-01-04 15:12:26
49JohnDoejohn@example.com2025-01-04 15:12:30
50JohnDoejohn@example.com2025-01-04 23:46:11
51DonaldDuckdduck@example.com2025-01-07 14:30:59
52DaisyDuckdaisyduck@example.com2025-01-07 14:30:59
53Plutopluto@example.com2025-01-07 14:30:59
54JohnDoejohn@example.com2025-01-07 14:50:22
55JohnDeerejohn@deere.net2025-01-07 18:49:25
56DonaldDuckdduck@example.com2025-01-10 07:32:28
57DaisyDuckdaisyduck@example.com2025-01-10 07:32:28
58Plutopluto@example.com2025-01-10 07:32:28
59JohnDoejohn@example.com2025-01-10 08:29:05
60JohnDeerejohn@deere.net2025-01-11 09:59:18
61DonaldDuckdduck@example.com2025-01-12 11:28:23
62DaisyDuckdaisyduck@example.com2025-01-12 11:28:23
63Plutopluto@example.com2025-01-12 11:28:23
64JohnDoejohn@example.com2025-01-12 14:18:21
65JohnDeerejohn@deere.net2025-01-14 05:19:18
66DonaldDuckdduck@example.com2025-01-15 13:03:58
67DaisyDuckdaisyduck@example.com2025-01-15 13:03:58
68Plutopluto@example.com2025-01-15 13:03:58

Here is the code for accessing the database and display the information.

<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th><th>Email</th><th>Registration Date</th></tr>";

class TableRows extends RecursiveIteratorIterator {
  function __construct($it) {
    parent::__construct($it, self::LEAVES_ONLY);
  }

  function current() {
    return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
  }

  function beginChildren() {
    echo "<tr style='width:150px;border:1px solid black;background-color:yellow;'>";
  }

  function endChildren() {
    echo "</tr>" . "\n";
  }
}

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Define and execute prepared statement
  $stmt = $conn->prepare("SELECT id, firstname, lastname, email, reg_date FROM MyGuests");
  $stmt->execute();

  // set the resulting array to associative
  $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
  foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
    echo $v;
  }
} catch(PDOException $e) {
  echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>