I tried to delete data out of PHPMyAdmin tables using PDO and it is not working. My connection is established. When I click on the delete button in the URL it would not pass in the ID. Any ideas how I can get it to work?
Here is the form:
<?php include_once 'admin/includes/helpers.inc.php';?>
<?php include 'index.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>List of Jokes</title>
</head>
<body>
<p><a href ="/comp1321_database/jokes/form.html.php/">Add your own joke</a></p>
<p>Here are all the jokes in the database</p>
<!-- into a table -->
<table border="1">
<?php foreach ($jokes as $joke): ?>
<form action="?deletejoke" method="POST">
<tr>
<td><?php html($joke['joketext']);?></td>
<td><?php $display_date = date("Y-m-d", strtotime($joke['jokedate']));
html($display_date); ?>
</td>
<td><img height="100px" src="../images/<?php html($joke['image']);?>" /></td>
<td><a href=" mailto:<?php html($joke['email']);?>">
<?php html($joke['name']);?></a></td>
</td>
<td><input type="hidden" name="id" value="<?php echo $joke['joke_id'];
?>">
<input type="submit" value="Delete"></td>
</tr>
</form>
<?php endforeach; ?>
</table>
<?php include 'admin/includes/footer.inc.html.php';?>
</body>
</html>
Here is my delete block:
if (isset($_POST['deletejoke'])) {
try
{
$sql = 'DELETE FROM joke WHERE joke_id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
} catch (PDOException $e) {
$error ='Error deleting joke' . $e->getMessage();
include 'error.html.pp';
exit();
}
header('Location: .');
exit();
}
Many thanks.
2
Answers
First of all you got an error in your code in your
catch
:should probably be:
Second thing is that you are not passing any value in
$_POST['deletejoke']
, as it is being sent as$_GET
if being sent at all when it has no value (depends of the browser)…So basically change this:
to this:
And…
to:
deletejoke
is the name of the form which is not passed through to$_POST
.This line:
Should be