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:

<!DOCTYPE>
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<style>
table {
border-collapse: collapse;
}

table, th, td {
border: 1px solid gray;
padding:5px;
}
tr:hover {background-color: #f5f5f5}
</style>

<head>
<body>
<?php
require_once '../include/functions.php';
require_once '../include/db.php';

$htmltable = "";

$htmltable .= "<table>
<tr>
<th>ID</th>
<th>Ime</th>
<th>Ime slavljenika</th>
<th>Datum</th>
<th>Poruka</th>
<th>Vreme prijave</th>
<th>Obrisi</th>
</tr>";


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

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

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

</tr>';
}

$htmltable.='</table>';

echo $htmltable;

?>

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

<script>
function email(){

$.ajax({
type:"post",
url: "email.php",

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

</body>
</html>


delete.php

<?php
include('../include/db.php');
include('../include/functions.php');



$prijavljeni_id=$_GET['prijavljeni_id'];


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


$result->execute();
header("location: izvestaj.php");

?>

Answer Source

You have issue in prepare statement

Change

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

To

$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