Milica Pavlovic Milica Pavlovic - 3 years ago 143
MySQL Question

How to take value of id from generated php table and delete row with that id

I have problem with delete function. I am trying to delete specific row from table, to allow admin to delete row he wants, row with certain id. I think there is problem in my script with getting value of id.
With these two scripts, I get deleted all the values from table when I click on delete option in any row, so thats why I think that I am having problem with taking value of id. Still couldnt solve this by myself so I decided to ask you, do you maybe see something that I dont?

report.php is a page with table that includes all the rows from database table, here is the code:

<link rel="stylesheet" href="">
<script src=""></script>
<script src=""></script>
table {
border-collapse: collapse;

table, th, td {
border: 1px solid gray;
tr:hover {background-color: #f5f5f5}

require_once '../include/functions.php';
require_once '../include/db.php';

$htmltable = "";

$htmltable .= "<table>
<th>Ime slavljenika</th>
<th>Vreme prijave</th>

$prep = $db->prepare("SELECT * from prijavljeni");
$prijavljeni = $prep->fetchAll();

foreach($prijavljeni as $prijavljen => $row) {

$htmltable.= '<tr>
<td>'.$row['ime'].' </td>
<td>'.$row['ime_slavljenik'].' </td>
<td>'.$row['datum'] .'</td>
<td>'.$row['poruka'].' </td>
<td><a href="delete.php?prijavljeni_id='.$prijavljeni["prijavljeni_id"].'">Delete</a></td>



echo $htmltable;


<button onclick="return email()">Posalji</button>
<div id="emailporuka">
<p align="center"><a href="logout.php">Logout</a></p>

function email(){

url: "email.php",

success: function(data){
document.getElementById("emailporuka").innerHTML = data;
error: function (req, status, err) {
console.log('Something went wrong', status, err);
return false;





$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id = prijavljeni_id");//brise sve reove
$result->bindParam('prijavljeni_id', $prijavljeni_id);//brise sve redove

header("location: izvestaj.php");


Answer Source

You have issue in prepare statement


$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id = prijavljeni_id");//brise sve reove
$result->bindParam('prijavljeni_id', $prijavljeni_id);


$result = $db->prepare("DELETE FROM prijavljeni WHERE prijavljeni_id = :prijavljeni_id");// Need to add : before bind param name//brise sve reove
$result->bindParam(':prijavljeni_id', $prijavljeni_id);

According to your code prijavljeni_id = prijavljeni_id so this condition become always true so your all records are being deleted

Also change <a> as below. add $row instead of $prijavljeni

<td><a href="delete.php?prijavljeni_id='.$row["prijavljeni_id"].'">Delete</a></td>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download