Donny Donny - 1 year ago 45
MySQL Question

Cannot delete record from mysql with this statement in php

I am having issue getting the record to delete. when i click the delete it goes through the motion no errors show up, but the record is still there. I am not sure what the issue is.

index.php

//connect to database with PDO
$db = new PDO ('mysql:host=localhost;dbname=database_here;charset=utf8mb4', 'username_here','password_here');

<div class="container">
<h2 class="panel panel-primary show_hide_e">E</h2>
<!--Create a query to find all records starting with a and loop through them and show them bellow-->
<?php $sql = "SELECT id, title, type, price, image, description, date FROM collection where title RLIKE '^[E]' ORDER BY type || title asc";
$stmt->execute();

?><?php //start of the while loop ?>
<?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) )
{
$image_id = $row['image'];
$id = $row['id'];
echo "<h4 class='slidingDiv_e'>".
'<img src="https://localhost/assets/images/'.$image_id.'" width="84" height="104" />'.
"<br>".
$row["type"].

" - Title:". $row["title"].

" / ".

$row["description"].

" / Price: ". $row["price"].

" / Date: ". $row["date"].

"<br>".
"<a class='btn btn-primary' href='https://localhost/update.php?id=$id'>Update</a>".
' '.
"<a class='btn btn-danger' href='https://localhost/delete.php?id=$id'>Delete</a>".
'<hr>'.
"</h4>";
}

?>
</div>


delete.php

//connect to database with PDO
$db = new PDO ('mysql:host=localhost;dbname=database_here;charset=utf8mb4', 'username_here','password_here');

//delete from database
$stmt=$db->prepare("DELETE FROM collection WHERE id=:id");
$stmt->bindParam(":id",$id,PDO::PARAM_INT);
$stmt->execute();

Answer Source
$stmt=$db->prepare("DELETE FROM collection WHERE id=:id");
$stmt->bindParam(":id",$_GET['id'],PDO::PARAM_INT);
$stmt->execute();

try this

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download