I have a user table where I have to display a delete button and I need to make sure the current user cannot delete themselves.
In order to do that, I wrote some code as shown below, but it doesn't work.
The user table appears with the delete button in all the users, but I need to make sure the current user cannot delete their own user account.
while($row = mysqli_fetch_array($result)) {
$output .='<tr>
<td>'.$row["user_id"].'</td>
<td>'.$row["fullname"].' </td>
<td>'.$row["user_role"].'</td>
<td>'.$row["username"].'</td>
<td>'.$row["password"].'</td>
<td>'.$row["branch_id"].'</td>
<td>'.$row["registered_date"].'</td>
<td>
<button id="'.$row["user_id"].'" class="btn btn-warning btn-xs edit_data"><i class="fa fa-edit "></i></button>
if('.$_SESSION['username'].'!='.$row["username"].') {
<a id="'.$row["user_id"].'" class="btn btn-danger btn-xs delete"><i class="fa fa-times"></i></a> } </td>
</tr>';
}
$output .= '</table>';
echo $output;
You can not execute PHP in a string, you need to split your string creation.
while ($row = mysqli_fetch_array($result)) {
$output .='<tr>
<td>'.$row["user_id"].'</td>
<td>'.$row["fullname"].' </td>
<td>'.$row["user_role"].'</td>
<td>'.$row["username"].'</td>
<td>'.$row["password"].'</td>
<td>'.$row["branch_id"].'</td>
<td>'.$row["registered_date"].'</td>
<td>
<button id="'.$row["user_id"].'" class="btn btn-warning btn-xs edit_data"><i class="fa fa-edit "></i></button>';
if ($_SESSION['username'] != $row["username"]) {
$output .= '<a id="'.$row["user_id"].'" class="btn btn-danger btn-xs delete"><i class="fa fa-times"></i></a>';
}
$output .= '</td></tr>';
}
$output .= '</table>';
echo $output;
Since you output your variable right after the loop - you can do the following
<table>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?=$row['user_id']; ?></td>
<td><?=$row['fullname']; ?></td>
<td><?=$row['user_role']; ?></td>
<td><?=$row['username']; ?></td>
<td><?=$row['password']; ?></td>
<td><?=$row['branch_id']; ?></td>
<td><?=$row['registered_date']; ?></td>
<td>
<?php
if($_SESSION['username'] != $row['username'])
{
?>
<a id="<?=$row['user_id']; ?>" class="btn btn-danger btn-xs delete"><i class="fa fa-times"></i></a>
<?php
}
?>
</td>
</tr>
<?php
}
?>
</table>
Related
I have a problem in the column referring to the column Actions of my listings table. The error is as follows.
Parse error: syntax error, unexpected token "<" in C:\xampp\htdocs\mangas\view\form_select_manga.php on line 42`
This error is caused by:
<a class="btn btn-info" title="Edit" href="edit_manga.php?id_manga=<?php echo $id_manga ?>" role="button"><i class="fa fa-edit"></i> Edit</a>
<a class="btn btn-danger" title="Delete" href="delete_manga.php?id_manga=<?php echo $id_manga ?>" role="button"><i class="fa fa-trash"></i> Delete</a>
Only the two action buttons have this problem, I have difficulties to adapt the table without causing an error.
<div class="container" style="margin-top: 40px;">
<h4 class="text-center">Manga list</h4>
<br>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Publisher</th>
<th>Volumes</th>
<th>Discont</th>
<th>Collection value</th>
<th>Registration date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
require_once "../model/MangaDAO.php";
$manga = new MangaDAO();
foreach ($manga->selectAllMangas() as $value){
echo '<tr>';
echo '<td title="'.$value['id_manga'].'">'.$value['id_manga'].'</td>';
echo '<td title="'.$value['title'].'">'.$value['title'].'</td>';
echo '<td title="'.$value['publisher'].'">'.$value['publisher'].'</td>';
echo '<td title="'.$value['volumes'].'">'.$value['volumes'].'</td>';
echo '<td title="R$'.$value['discont'].'">R$'.$value['discont'].'</td>';
echo '<td title="R$'.$value['value'].'">R$'.$value['value'].'</td>';
echo '<td title="'.date('d/m/Y H:i:s', strtotime($value['registration_date'])).'">'.
date('d/m/Y H:i:s', strtotime($value['registration_date'])).'</td>';
<a class="btn btn-info" title="Edit" href="edit_manga.php?id_manga=<?php echo $id_manga ?>" role="button"><i class="fa fa-edit"></i> Edit</a>
<a class="btn btn-danger" title="Delete" href="delete_manga.php?id_manga=<?php echo $id_manga ?>" role="button"><i class="fa fa-trash"></i> Delete</a>
echo '</tr>'; echo '</p>';
}
?>
</tbody>
</table>
</div>
<?php
require_once "../model/MangaDAO.php";
$manga = new MangaDAO();
foreach ($manga->readAllMangas() as $value){
echo '<tr>';
echo '<td title="'.$value['id_manga'].'">'.$value['id_manga'].'</td>';
echo '<td title="'.$value['title].'">'.$value['title'].'</td>';
echo '<td title="'.$value['publisher'].'">'.$value['publisher'].'</td>';
echo '<td title="'.$value['volumes'].'">'.$value['volumes'].'</td>';
echo '<td title="R$'.$value['discount'].'">R$'.$value['discount'].'</td>';
echo '<td title="R$'.$value['value'].'">R$'.$value['value'].'</td>';
echo '<td title="'.date('d/m/Y H:i:s', strtotime($value['registering_date'])).'">'.
date('d/m/Y H:i:s', strtotime($value['registering_date'])).'</td>';
echo '<td>';
echo '<a class="btn btn-info" title="Update '.$value['title'].'"'.'href="/mangas/view/form_update_manga.php?id_manga='.$value['id_manga'].'"role="button" ><i class="fa fa-edit"></i> Update</a>';
echo " ";
echo '<a class="btn btn-danger" title="Delete '.$value['title'].'"'.'href="/mangas/controller/delete_manga.php?id_manga='.$value['id_manga'].'"role="button"><i class="fa fa-trash"></i> Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
I'm creating a table using ajax and php but there's one problem, the table isn't showing in my div. I'm really new to ajax so I don't really fully understand it yet.
Here's my div:
<div class="body" id="live-data">
</div>
Here's the ajax code:
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
});
And here's fetch.php:
<?php
include('../global/db.php');
$output = '';
$sql ="SELECT * FROM students WHERE status = '0' AND stud_grade = '$level_id' ORDER BY date_enrolled DESC";
$result = mysqli_query($db, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>';
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
$output .= '
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
';
}
}
else {
$output .= '
<tr>
<td colspan="12">Data not Found</td>
</tr>';
}
$output .= '
</tbody>
</table>
</div>';
echo $output;
?>
It would great if anyone could help because I just don't know why it doesn't work
Edit:
I've changed the code so it returns the data in the console tab and here's what shows up:
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
</tbody>
</table>
</div>
So it clearly returns the correct data but it just doesn't show up in the live-data div
Maybe take a look in your html where is your div. Your div id says "live-data" and in the ajax code you mentioned to fetch data for div id "#live_data" instead of "#live-data".
Maybe changing them for same name can solve your problem. I would use for thr div id and in the ajax same id names like "#liveData".
Example (same code, just edited to the right IDs, compare with your original):
Your HTML div
<div class="body" id="liveData">
</div>
Your ajax code
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#liveData').html(data);
}
});
}
fetch_data();
});
Just append the correct Id of the div tag in sucess function
I have created a while loop that will call data from the database and each row will come with a button however when I click one button for update it updates for all rows. I did some research however the increment solution seems not to make any difference on my quote.
<form action="registered.php" method="GET">
<?php
$sql = "SELECT id, First_name, Last_name, email, Country, paid FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$i = 0;
$id = $row['id'];
?>
<tr>
<td>
<?= $row['First_name'];?>
</td>
<td class="hidden-phone"><?= $row['Last_name'];?></td>
<td><?= $row['email'];?></td>
<td><?= $row['Country'];?></td>
<td><span class="label label-info label-mini"><?= $row['paid'];?></span></td>
<td>
<button class="btn btn-success btn-xs auth" name="$i"><i class="fa fa-check" title="Authorize Access"></i></button>
<button class="btn btn-primary btn-xs" name="$id"><i class="fa fa-times" title="Remove Access"></i></button>
<button class="btn btn-danger btn-xs" name="$id"><i class="fa fa-trash-o " title="Delete user"></i></button>
</td>
</tr>
<?php
if (isset($_POST['$i'])) {
$sql = "UPDATE users SET paid = 'ads' WHERE id = '$id'";
$update = $conn->query($sql);
}
$i++;
}
} else {
echo "0 results";
}
$conn->close();
?>
</form>
You can give each button a value. I would also suggest give them different names and better names than $i (was this meant to be PHP variable?).
As a side note:
Your code was vulnerable to SQL injection. My example uses prepared statements with parameter binding.
Don't mix HTML and PHP. I kept the first query separate on purpose. You should perform PHP code before you start outputting HTML.
I moved <form> iside <td>. You can put <form> as a child of <table>. However, getting rid of the form completely and using links could be better in your situation, unless you plan to use POST method.
<?php
$users = $conn->query('SELECT id, First_name, Last_name, email, Country, paid FROM users')->fetch_all(MYSQLI_ASSOC);
?>
<?php foreach ($users as $row): ?>
<tr>
<td>
<?= $row['First_name']; ?>
</td>
<td class="hidden-phone"><?= $row['Last_name']; ?></td>
<td><?= $row['email']; ?></td>
<td><?= $row['Country']; ?></td>
<td><span class="label label-info label-mini"><?= $row['paid']; ?></span></td>
<td>
<form action="registered.php" method="GET">
<button class="btn btn-success btn-xs auth" name="myButton" value="<?= $row['id']; ?>"><span class="fa fa-check"
title="Authorize Access"></span></button>
<button class="btn btn-primary btn-xs" name="removeAccess"><span class="fa fa-times" title="Remove Access"></span></button>
<button class="btn btn-danger btn-xs" name="deleteUser"><span class="fa fa-trash-o " title="Delete user"></span></button>
</form>
</td>
</tr>
<?php endforeach; ?>
Then in your registered.php you can execute UPDATE query based on the value received.
<?php
include 'mysqli.php';
if (isset($_GET['myButtton'])) {
$stmt = $conn->prepare("UPDATE users SET paid='ads' WHERE id=?");
$stmt->bind_param('s', $_GET['myButton']);
$stmt->execute();
}
<div class="container">
<h3 class="text-center">Hotel/Admin Joint Panel</h3>
<table class="table table-bordered">
<tr>
<th>Food_ID</th>
<th>Cuisines</th>
<th >Description</th>
<th >Price</th>
<th >Image</th>
<th >Date</th>
<th width="50px">Action</th>
</tr>
<?php
include_once 'mainclass.php';
$confirmThread = new menuDatabase();
$sql = "SELECT * FROM gallery";
$users = mysqli_query($confirmThread->getCon(),$sql);
while($user = $users->fetch_assoc()){
?>
<tr id="<?php echo $user['idGallery'] ?>" >
<td ><?php echo $user['idGallery'] ?></td>
<td><?php echo $user['titleGallery'] ?></td>
<td><?php echo $user['descGallery'] ?></td>
<td><?php echo $user['price'] ?></td>
<!-- <td><?php echo ("data:image/jpeg;base64,".base64_encode($user['image'] )) ?></td> -->
<td><?php echo '<img src="data:image/jpeg;base64,'.base64_encode($user['image'] ).'">'
?></td>
<td width="100px"><?php echo $user['samaye'] ?></td>
<td>
<button class="btn btn-danger btn-sm remove">Delete</button>
<button style="margin-top: 2px" id="createOnly" class="btn btn-danger"> Create Menu</button>
<button style="margin-top: 2px" id="updateOnly" id="hi" class="btn btn-danger btn-sm "> <a class="update" href="upload.php">Update</a></button>
</td>
</tr>
<?php } ?>
</table>
</div> <!-- container / end -->
I wanted to update the database using an update button which is in Menu.php. What I wanted is, when I clicked on the update button(in menu.php) it should grab the food_id(primary key) and remember this for Upload.php, from where I am doing real updating by getting all new values.
You can change the following statement to
<td>
<button class = "btn btn-danger btn-sm remove">Delete</button>
<button style = "margin-top: 2px" id = "createOnly" class = "btn btn-danger"> Create Menu</button>
<button style = "margin-top: 2px" id = "updateOnly" id = "hi" class = "btn btn-danger btn-sm"> <a class = "update" href = "upload.php">Update</a></button>
</td>
this. This will act as a form (Only for the update button) and you can get these values to your update.php. The id that you need is hidden. From update.php you can do what you need to.
<td>
<button class = "btn btn-danger btn-sm remove">Delete</button>
<button style = "margin-top: 2px" id = "createOnly" class = "btn btn-danger"> Create Menu</button>
<form method = "post" action = "upload.php">
<input type = "hidden" name="food_id" value="<?php echo $user['idGallery']; ?>"/>
<button type = "submit" style = "margin-top: 2px" id = "updateOnly" id = "hi" class = "btn btn-danger btn-sm">Update</button>
</form>
</td>
Or else you can do this by using Query Parameter like this,
<td>
<button class = "btn btn-danger btn-sm remove">Delete</button>
<button style = "margin-top: 2px" id = "createOnly" class = "btn btn-danger"> Create Menu</button>
<button style = "margin-top: 2px" id = "updateOnly" id = "hi" class = "btn btn-danger btn-sm"> <a class = "update" href = "upload.php?foodID=<?php echo $user['idGallery']; ?>">Update</a></button>
</td>
and from your update.php you can get the result like this,
$foodID = $_GET["foodID"];
Hope this helps you!
Hello i just new to coding but i don't know how to add span on my table on my echo every-time keep moving the table i need help how to do syntax on my code
here is my original code for my table
<td><span id="name<?php echo $row['id']; ?>"><?php echo $row['name']; ?></td>
<td><span id="email<?php echo $row['id']; ?>"><?php echo $row['email']; ?></td>
<td><span id="username<?php echo $row['id']; ?>"><?php echo $row['username']; ?></td>
<td><span id="level<?php echo $row['id']; ?>"><?php echo $row['password']; ?></td>
this my other table for my edit but everytime i press the button it does not display any value in my edit
<td>'.$row["name"].'</td>
<td>'.$row["username"].'</td>
<td>'.$row["email"].'</td>
<td>'.$row["level"].'</td>
<td><a style="cursor:pointer;" class="btn btn-info edit" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-edit" aria-hidden="true" ></span></a> || <a style="cursor:pointer;" class="btn btn-danger delete" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
what im trying to get how do i put id on this echo output to direct it to my button showing the value of id return all the value on that id.
while($row = mysqli_fetch_array($result)) {
$output .= '
<tr>
<td>'.$row["name"].'</td> <-- how do i add <span>,put id on this rows
<td>'.$row["username"].'</td> or how to add id on this table
<td>'.$row["email"].'</td>
<td>'.$row["level"].'</td>
<td>
<a style="cursor:pointer;" class="btn btn-info edit" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-edit" aria-hidden="true" ></span></a> || <a style="cursor:pointer;" class="btn btn-danger delete" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
</td>
</tr>';
}
echo $output;
}
Here's your $output with <span>s in them:
$output .= '
<tr>
<td><span id="name'.$row["id"].'">'.$row["name"].'</span></td>
<td><span id="username'.$row["id"].'">'.$row["username"].'</span></td>
<td><span id="email'.$row["id"].'">'.$row["email"].'</span></td>
<td><span id="level'.$row["id"].'">'.$row["level"].'</span></td>
<td>
<a style="cursor:pointer;" class="btn btn-info edit" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-edit" aria-hidden="true" ></span></a> || <a style="cursor:pointer;" class="btn btn-danger delete" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
</td>
</tr>';