Basically I have this code where I want to extract some data from a database and display it on a pop-up window. The problem is when I click on the eye icon ( labelled More ) I want it to display the correspondent id of that table row but I can't seem to figure out how to do that.
For more references I set up a temporary website to better see the problem : http://twgtest-org.stackstaging.com/bau50/bau50_extract.php
You can see that when I click on more the displayed ID is actually all the IDs from the database instead of it being only the ID of that row
<?php
//load database connection
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
<ul class="text-align">
<?php
if (!$query->rowCount() == 0) {
while ($show_all = $query->fetch()) {
echo '<tr><th scope="row">';
echo $show_all['id_Bau50'];
echo '</th><td>';
echo $show_all['name'];
echo '</td><td>';
echo $show_all['produkt'];
echo '</td><td>';
echo $show_all['preis']," €";
echo '</th><td>';
echo $show_all['Ergebniss'];
echo '</td>
<td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-title="more" data-toggle="modal" data-target="#more" ><span class="fa fa-eye"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';
}
echo '</tbody></table>';
}
?></ul>
</div>
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
echo'<div class="modal fade" id="more" tabindex="-1" role="dialog" aria-labelledby="more" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fa fa-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">More</h4>
</div>
<div class="modal-body">
<div>';
while ($show= $query2->fetch()) {
$id= $show ['id_Bau50'];
echo $id;
}
echo '</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</div>';
?>
This is the very simply solution: add a new modal for each row identified by its id for example for id "4" you will open the modal with id "modal4" and so on:
<?php
//load database connection
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
<ul class="text-align">
<?php
if (!$query->rowCount() == 0) {
while ($show_all = $query->fetch()) {
echo '<tr><th scope="row">';
echo $show_all['id_Bau50'];
echo '</th><td>';
echo $show_all['name'];
echo '</td><td>';
echo $show_all['produkt'];
echo '</td><td>';
echo $show_all['preis']," €";
echo '</th><td>';
echo $show_all['Ergebniss'];
echo '</td>
<td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-title="more" data-toggle="modal" data-target="#more'.$show_all['id_Bau50'].'" ><span class="fa fa-eye"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';
}
echo '</tbody></table>';
}
?></ul>
</div>
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
while ($show= $query2->fetch()) {
$id= $show ['id_Bau50'];
echo'<div class="modal fade" id="more'.$id.'" tabindex="-1" role="dialog" aria-labelledby="more" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fa fa-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">More</h4>
</div>
<div class="modal-body">
<div>';
echo $id;
echo '</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</div>';
}
?>
This is ok for not so many rows or if you don't want to write some javascript.
A nicer solution would be to use one single modal with all content inside and show/hide the selected one, for this you need to use some js code and don't use the standard bootstrap attribute-driven methods.
In this example I'm using jquery and js code (se at the bottom).
<?php
//load database connection
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
<ul class="text-align">
<?php
if (!$query->rowCount() == 0) {
while ($show_all = $query->fetch()) {
echo '<tr><th scope="row">';
echo $show_all['id_Bau50'];
echo '</th><td>';
echo $show_all['name'];
echo '</td><td>';
echo $show_all['produkt'];
echo '</td><td>';
echo $show_all['preis']," €";
echo '</th><td>';
echo $show_all['Ergebniss'];
echo '</td>
<td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-moreid="'.$show_all['id_Bau50'].'" ><span class="fa fa-eye"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';
}
echo '</tbody></table>';
}
?></ul>
</div>
<div class="modal fade" id="more" tabindex="-1" role="dialog" aria-labelledby="more" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fa fa-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">More</h4>
</div>
<div class="modal-body">
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
while ($show= $query2->fetch()) {
$id= $show ['id_Bau50'];
echo '<div id="more'.$id.'">'.$id.'</div>';
}
?>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- YOU NEED JQUERY FOR THIS: -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$(function(){
$("button[data-moreid]").click(function(){
$("#more .modal-body > div").hide(); //hide all more div
var id=$(this).attr("data-moreid"); //get id from pushed button
$("#more .modal-body #more"+id).show(); //show only pushed id
$("#more").modal("show"); //show modal
});
});
</script>
Related
I'm creating a web back end for a restaurant. There is an option to block/unblock restaurant owners. I have put an "if" condition to appear button (a tag) according to the status of the restaurant owner. Within this "a" tag I want to pass the value (id) to a modal box.
I put some code in the "data-target" of "a" tag, as well as in the modal id. The value didn't pass and also didn't open the modal.
<tbody>
<?php
global $con;
$sql = "SELECT * FROM `fd_owner_details`";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
$kk=$row['id'];
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>
<td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>
<td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>
<td><?php echo '<img src= "'.$row['image'].'">'; ?></td>
<td>
<?php
if ($row['status']==1){
echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
}
else {
echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
}
?>
</td>
<td>
<span>
<i class="fa fa-pencil" aria-hidden="true"></i>
<?php
if ($row['status']==1){
echo '<i class="fa fa-ban" aria-hidden="true"></i>';
}
else {
echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
}
?>
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</td>
</tr>
<?php } ?>
</tbody>
--------------------------------------------------------------------------------
<div class="modal fade" id="blockRestaurantOwner?id=<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="blockRestaurantOwnerTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">× </span>
</button>
</div>
<div class="modal-body">
<p id="main-content">Are you sure you want to block this restaurant owner ???</p>
<p id="content">You will not be able to recover this action !!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
<button type="button" class="btn btn-warning">Yes, Block</button>
</div>
</div>
</div>
</div>
Where Should I change, to get the "id" to the modal ?
Just copy the whole script and try to past inside a php file and try to run it. This can be modified as expected at your end using while loop.
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<?php
$array[] = array('id'=>1, 'fname'=>'hari','lname'=>'ltest','address_line1'=>'no:9','address_line1'=>'rajan street','contact_no1'=>'92222','contact_no2'=>'899782','status'=>1);
$array[] = array('id'=>2, 'fname'=>'vvvv','lname'=>'ltest2','address_line1'=>'no:92','address_line1'=>'siv street','contact_no1'=>'522','contact_no2'=>'2922','status'=>0);
?>
<table border=1>
<tbody>
<?php foreach($array as $row){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>
<td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>
<td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>
<td>
<?php
if ($row['status']==1){
echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
}
else {
echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
}
?>
</td>
<td>
<span>
<i class="fa fa-pencil" aria-hidden="true"></i>
<?php if ($row['status']==1){ ?>
<a data-toggle="modal" href="#blockRestaurantOwner<?=$row['id']?>" class="btn btn-warning btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="Block"><i class="fa fa-ban" aria-hidden="true"></i></a>
<div id="blockRestaurantOwner<?php echo $row['id']; ?>" class="modal fade" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">× </span>
</button>
</div>
<div class="modal-body">
<p id="main-content">Are you sure you want to block this restaurant owner ???</p>
<p id="content">You will not be able to recover this action !!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
<button type="button" class="btn btn-warning">Yes, Block</button>
</div>
</div>
</div>
</div>
<?php }
else {
echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
}
?>
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</td>
</tr>
<?php } ?>
</tbody>
</table>
you can set onclick listener to block button and pass owner id to that function like below
<tbody>
<?php
global $con;
$sql = "SELECT * FROM `fd_owner_details`";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
$kk=$row['id'];
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>
<td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>
<td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>
<td><?php echo '<img src= "'.$row['image'].'">'; ?></td>
<td>
<?php
if ($row['status']==1){
echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
}
else {
echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
}
?>
</td>
<td>
<span>
<i class="fa fa-pencil" aria-hidden="true"></i>
<?php
if ($row['status']==1){
echo '<i class="fa fa-ban" aria-hidden="true"></i>';
}
else {
echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
}
?>
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</td>
</tr>
<?php } ?>
</tbody>
<div class="modal fade" id="blockRestaurantOwner" tabindex="-1" role="dialog" aria-labelledby="blockRestaurantOwnerTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">× </span>
</button>
</div>
<div class="modal-body">
<p id="main-content">Are you sure you want to block this restaurant owner ???</p>
<p id="content">You will not be able to recover this action !!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
<button type="button" class="btn btn-warning">Yes, Block</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var target_owner_id="";
function set_target_id(id){
target_owner_id = id;
console.log(target_owner_id);
}
</script>
Currently i have a table that is generated via DB and i want to view the details of the selected value on the data table. I separated the modal with view/users/modal. but there's an error
Message: Trying to get property 'user_id' of non-object
please help me to resolve this
I am running Codeigniter 3.1.10
Xampp V3.2.3
Data Table
<?php echo $this->load->view('users/Modal/view_modal'); ?>
<tbody>
<?php if(!empty($value)): ?>
<?php foreach($value as $row): ?>
<tr>
<td align="center"><?php echo $row->user_id; ?></td>
<td align="center"><?php echo $row->firstname; ?></td>
<td align="center"><?php echo $row->lastname; ?></td>
<td align="center"><?php echo $row->email; ?></td>
<td align="center">
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#info_modal<?php echo $row->user_id; ?>">
<i class="far fa-eye"></i>
</button>
<a href="<?php echo base_url('users/view_edit_form/'.$row->user_id); ?>" class="btn btn-success">
<i class="fas fa-user-edit"></i>
</a>
<a href="" class="btn btn-danger">
<i class="fas fa-ban"></i>
</a>
</td>
<?php endforeach; ?>
My Modal
<div class="modal fade" id="info_modal<?php echo $row->user_id; ?> " tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"><?php echo $row->firstname; ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
I expect the output will display more information of the user but the actual output is an error message Message: Trying to get property 'user_id' of non-object
try to check what $row contains.
If -> don't work, try using $row['user_id']
I have a delete button in a data table like this:
<div class="card mb-3" style="width:70%;">
<div class="card-header">
<i class="fas fa-table"></i>
Perusahaan</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Kode Perusahaan</th>
<th>Nama Perusahaan</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$conn = mysqli_connect("127.0.0.1","root","","penggajian");
$sql = "SELECT * FROM perusahaan";
$getperusahaan = mysqli_query($conn,$sql);
while($list = mysqli_fetch_array($getperusahaan, MYSQLI_ASSOC)){
echo "<tr><td>";
echo $list['kode'];
echo "</td><td>";
echo $list['nama'];
echo "</td><td>";
?>
<button type="button" data-id="<?php echo $list['id_perusahaan']; ?>" class="btn btn-primary btn-sm passingID" data-toggle="modal" data-target="#editperusahaan"><i class="fas fa-pencil-alt"></i> Edit</button></a>
<button type="button" data-toggle="modal" data-target="#konfirmasi" class="btn btn-danger btn-sm delete" style="margin-left: 10px;"><i class="far fa-times-circle"></i> Delete</button>
</td></tr>
<?php
}
?>
</tbody>
</table>
</div>
when i click the delete button i want to make a confirmation on a bootstrap modal, before the user can delete it. This is my modal:
<div class="modal fade" id="konfirmasi" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Konfirmasi Penghapusan Data</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Hapus Data?</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
Hapus
</div>
</div>
</div>
</div>
The problem is that i don't know how to passed the $list['id'] from the data table to my modal
Replace this in your table list
<button data-id="<?php echo $list['id'];?>" type="button" data-toggle="modal" data-target="#konfirmasi" class="btn btn-danger btn-sm delete-button" style="margin-left: 10px;"><i class="far fa-times-circle"></i> Delete</button>
Replace this in your modal popup HTML
<a data-id="" class="btn btn-danger confirm-delete">Hapus</a>
Add javascript code
$('.delete-button').on('click', function (e) {
var id = $(this).attr('data-id');
$('.confirm-delete').attr('data-id',id);
});
$(".confirm-delete").on('click', function (e) {
var id = $(this).attr('data-id');
console.log(id);
location.href="hapusperusahaan.php?id="+id;
});
for test
http://jsfiddle.net/b8m03kjr
try this:
button class="btn btn-danger btn-sm remove">Delete</button>
//delete.php
$id = $_GET['id'];
//Connect DB
$conn = mysqli_connect("127.0.0.1","root","","penggajian");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM perusahaan WHERE id = $id";
echo"sucess message";
//js
$(".remove").click(function(){
var id = $(this).parents("tr").attr("id");
if(confirm('Are you sure to delete this record ?'))
{
$.ajax({
url: '/delete.php',
type: 'GET',
data: {id: id},
error: function() {
alert('Something is wrong');
},
success: function(data) {
$("#"+id).remove();
alert("Record deleted successfully");
}
});
}
});
I'm very noob in PHP, but i'm really stuck here trying to figure out how to delete a row printed in a while loop here :
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=".$_SESSION["ID"]."";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result3)) {
echo "<h4>" . $row["exp_title"]. "<a href='#' title=''><i class='fa fa-pencil'></i><i onclick='location.href='userprofile.php?deleteID=".$row["auto_id"]."';' class='fa fa-minus-square' data-toggle='modal' data-target='#EXPDELModal' value='delete_exp'></i></a></h4>";
echo "<p>" . $row["exp_detail"]. "</p>";
}
}
else
{
echo "<div class='textfaded2'>Add you experience here.</div>";
}
?>
Note: i want to delete the row using a font-awesome icon and i'm using
'auto_id' as the auto_increment, primary key to define the row i want
to delete.
Here is the code for the modal:
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php?deleteID=<?php '.$row["auto_id"].';?>"
style="width: 100%;" method="post" value="delete_exp">
<button type="button" name="delete" class="btn btn-default" data-
dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-
dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>
And finally the query for delete:
<?php
if(isset($_POST['delete']))
{
$sql6="DELETE FROM `user_exp` WHERE auto_id=".$_GET['deleteID']."";
$result=mysqli_query($conn,$sql6) or die();
}
?>
I would like to thank you for taking the time to read this.
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=" . $_SESSION["ID"] . "";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
while ($row = mysqli_fetch_assoc($result3)) {
?>
<h4><?php echo $row["exp_title"]; ?>
<a href='#' title=''>
<i class='fa fa-pencil'></i>
<i onclick='setValue("<?php $row["auto_id"]; ?>");' class='fa fa-minus-square' data-toggle='modal'
data-target='#EXPDELModal'></i>
</a>
</h4>
<p><?php echo $row["exp_detail"]; ?></p>;
<?php
}
} else {
echo "<div class='textfaded2'>Add you experience here.</div>";
}
if (isset($_POST['delete'])) {
if($_POST['deleteID']) {
$sql6 = "DELETE FROM `user_exp` WHERE auto_id=" . $_POST['deleteID'] . "";
$result = mysqli_query($conn, $sql6) or die();
}
}
?>
<script>
function setValue(value) {
document.getElementById("deleteId").value = value;
}
</script>
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php" style="width: 100%;" method="post" name="delete_exp">
<input type="hidden" value="0" name="deleteID" id="deleteId" readonly/>
<button type="submit" name="delete" class="btn btn-default" data-dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>
i'm trying to delete database record inside a while loop. I'm showing my user list in a table with while loop. I have a button, bootstrap modal opening a modal window. In that window i have submit to delete button. With while loop.
Problem is, i'm trying to delete this record, but its deleting random record. can you check is there a problem?
Thanks already.
here is my code:
<table class="table table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>*</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users";
$r = mysqli_query($dbc,$q);
while($list = mysqli_fetch_assoc($r)){
if(isset($_POST['del_submit'])){
$q = "DELETE FROM users WHERE id = '$list[id]' ";
$r = mysqli_query($dbc, $q);
header('Location: index.php?page=7');
}
echo '<tr>';
echo '<td>'.$list['id'].'</td>';
echo '<td>'.$list['name'].'</td>';
echo '<td>'.$list['surname'].'</td>';
echo '<td>'.$list['email'].'</td>';
echo '<td>'.$list['password'].'</td>';
echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> ';
echo '</tr><form method="post" action="#">';
echo '<div class="modal fade delete'.$list['id'].'">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4>
</div>
<div class="modal-body">
<strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br>
Are you Sure?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" name="del_submit" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
</div>
</div>
</div>
</div></form>';
}
?>
</tbody>
</table>
Create a delete page and link to it.
first you need to add link to the same page with action and id parameters
<?php
// replace
?>
<button type="submit" name="del_submit" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
<?php
// with this:
?>
<input type='hidden' name='id' value='<?= $list['id']; ?>'>
<input type='hidden' name='action' value='delete'>
<button class="btn btn-danger" type='submit'><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
add this to the top of your page
<?php
if(isset($_POST['id'], $_POST['action']) && $_POST['action'] === 'delete')
{
$id = $_POST['id'];
$query = 'DELETE FROM `users` WHERE `id` = ?';
$db = new Mysqli('localhost','user','password','database');
$stmt = $db->prepare($query);
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->close();
$db->close();
echo 'Deleted! (if exists)';
}
i will first acknowledge that your script is not safe and efficient this way. Why not handle delete operations with Ajax and call to a different script?
You have to pass the value of the id with the delete button and then use the id value you passed with the button instead.
For your delete button inside the modal add value attribute
<button type="submit" name="del_submit" value="'.$list['id'].'" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
Then retrieve the id and use the id. Its should work
if(isset($_POST['del_submit'])){
$listItemID = $_POST['del_submit'];
$q = "DELETE FROM users WHERE id = '$listItemID' ";
$r = mysqli_query($dbc, $q);
header('Location: index.php?page=7');
}
If where you are redirecting the user is with the id of the deleted element simply use
`header('Location: index.php?page=$listItemID'); instead of header('Location: index.php?page=7');
Full code becomes
<table class="table table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>*</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users";
$r = mysqli_query($dbc,$q);
while($list = mysqli_fetch_assoc($r)){
if(isset($_POST['del_submit'])){
$listItemID = $_POST['del_submit'];
$q = "DELETE FROM users WHERE id = '$listItemID' ";
$r = mysqli_query($dbc, $q);
header('Location: index.php?page=7');
}
echo '<tr>';
echo '<td>'.$list['id'].'</td>';
echo '<td>'.$list['name'].'</td>';
echo '<td>'.$list['surname'].'</td>';
echo '<td>'.$list['email'].'</td>';
echo '<td>'.$list['password'].'</td>';
echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> ';
echo '</tr><form method="post" action="#">';
echo '<div class="modal fade delete'.$list['id'].'">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4>
</div>
<div class="modal-body">
<strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br>
Are you Sure?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" name="del_submit" value="'.$list['id'].'" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
</div>
</div>
</div>
</div></form>';
}
?>
</tbody>
</table>