I have a list.php which displays all the records that is in the database with 2 options Edit or Delete. When clicked its suppose to delete by id but instead it deleted everything. I'm not sure where i went wrong, please help.
list.php
<?php
include "db.php";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM myguestbook");
$stmt->execute();
$result = $stmt->fetchAll(); //fetchAll() function will grab the query result and store it in an associative array $result. grab more than one record.
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
<!DOCTYPE html>
<html>
<head>
<title>My Guestbook</title>
</head>
<body>
<ol>
<?php
foreach($result as $row) {
echo "<li>";
echo "Name : ".$row["user"]."<br>";
echo "Email : ".$row["email"]."<br>";
echo "Date : ".$row["postdate"]."<br>";
echo "Time : ".$row["posttime"]."<br>";
echo "Comments : ".$row["comment"]."<br>";
echo "Action : <a href=edit.php?id=".$row["id"].">Edit</a> / <a href=delete.php?id=".$row["id"].">Delete</a>";
echo "</li>";
echo "<hr>";
}
?>
</ol>
</body>
</html>
delete.php
<?php
if (isset($_GET['id'])) {
include "db.php";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("DELETE FROM myguestbook WHERE id = :record_id");
$stmt->bindParam(':record_id', $id, PDO::PARAM_INT);
$id = $_GET['id'];
$stmt->execute();
header("Location:list.php");
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
}
else {
echo "Error: You have execute a wrong PHP. Please contact the web administrator.";
die();
}
?>