Delete table row php code - php

What is the problem with my code?
My PHP is working, but delete button doesn't work!
if(isset($_POST['delete'])) {
$ID = $_POST['value'];
$delete = "DELETE FROM tbl_document WHERE ID = $ID";
$result = mysqli_query($con,$delete);
}
$query = "SELECT * FROM tbl_document LIMIT $start, $end";
$result = mysqli_query($con,$query);
echo "<table border='1' width='300' height='160' align = center id='result'>";
echo '<tr>
<th width="80">ID</th>
<th width="200">Title</th>
<th width="260">Presented To</th>
<th width="260">Presented By</th>
<th width="160">Date Submitted</th>
<th>Location</th>
<th width="17%">Option</th>
</tr>';
while($row = mysqli_fetch_array($result)){
echo "<tr align = center >";
<td width='20' height='60'>" .$row['ID']. "</td>";
<td width='120' height='60'>" .$row['Title']. "</td>";
<td>" .$row['Presented_To']. "</td>";
<td>" .$row['Presented_By']. "</td>";
<td>" .$row['Date_Submitted']. "</td>";
<td>" .$row['Location']. "</td>";
"<td width='17%'>";
?>
<?php if($_SESSION['user'] == "1")
?>
<button class="w3-btn w3-red w3-border-large w3-circle" value="<?php echo $row['ID'];?>" name="delete" style="width:40%"><i class="fa fa-trash-o"></i>Delete</button>
<?php } ?>
<?php
echo "</td>";
echo "</tr>";
}
echo"</table>";

A button is not enough to send data, you need to contain it within a form.
Here is how it should look like:
<form method="post">
<button name="delete" value="<?php echo $row['ID'];?>" class="w3-btn w3-red w3-border-large w3-circle" style="width:40%"><i class="fa fa-trash-o"></i>Delete</button>
</form>

Related

I want to display "No Image" when there's no image 'BLOB' at MySQL database

based on my question above, currently, when there is no image at the database, it will display a broken image while if there is an image, it will display the image. Now how to display "No Image" when there's no image 'BLOB' at MySQL database.
I uses BLOB format at the MySQL Database. Below is the code:
<?php
$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() == 0){
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>
<th>Task Name</th>
<th>Before Task</th>
<th>After Task</th>
<th>OT From</th>
<th>OT To</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >
<tr>
<td colspan='8'>No report at this moment</td>
</tr>
</tbody>
</table>";
}else{
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>
<th>Task Name</th>
<th>Before Task</th>
<th>After Task</th>
<th>OT From</th>
<th>OT To</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >";
$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
while($row = $query->fetch(PDO::FETCH_ASSOC)){
echo "<tr>";
echo "<td rowspan='2'>". $row['report_date'] . "</td>";
echo "<td rowspan='2'>". $row['task_name'] . "</td>";
echo "<td align='center'><img src='data:image/jpeg;base64,".$before."'/></td>";
echo "<td align='center'><img src='data:image/jpeg;base64,".$row['photo_after']."'/></td>";
echo "<td rowspan='2' align='center'>". $row['ot_start']. "</td>";
echo "<td rowspan='2' align='center'>".$row['ot_end']. "</strong></td>";
echo "<td rowspan='2'><strong>". $row['report_status'] . "</strong></td>";
echo "<td rowspan='2' align='center'>";
echo "<a class='btn-view btn-primary btn-sm' href='../view_task/view_task.php?report_id=". $row['report_id'] ."' data-toggle='tooltip'>View</a>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'>". $row['time_photo_before'] . "</td>";
echo "<td align='center'>". $row['time_photo_after'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table><br>";
echo "</div>";
echo "<div>";
}
?>
Just put a condition to check whether the image exist or not empty in the row.
if(!isset($row['photo_after']) || empty($row['photo_after'])) {
echo "<td align='center'><img src='default.png'/></td>";
}
else
{
echo "<td align='center'><img src='data:image/jpeg;base64,".$row['photo_after']."'/></td>";
}
Try this..., I hope this will work for you.
Inside your while loop, I made a condition which checks there is something in your field or it's empty. if it's empty a variable store the text you want to print and if it's not empty it will show your image in variable $image and I echo the variable where you want to show your image.
<?php
$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() == 0){
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>
<th>Task Name</th>
<th>Before Task</th>
<th>After Task</th>
<th>OT From</th>
<th>OT To</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >
<tr>
<td colspan='8'>No report at this moment</td>
</tr>
</tbody>
</table>";
}else{
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>
<th>Task Name</th>
<th>Before Task</th>
<th>After Task</th>
<th>OT From</th>
<th>OT To</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >";
$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
while($row = $query->fetch(PDO::FETCH_ASSOC)){
// Here is my code...
if(empty($row['photo_after'])) {
$image = "NO IMAGE FOUND..."; //anything you want to diaplay if there is no image
}
else{ $image = "<img src='data:image/jpeg;base64,".$row['photo_after']."'/>"; }
// Check the $image variable in your table
echo "<tr>";
echo "<td rowspan='2'>". $row['report_date'] . "</td>";
echo "<td rowspan='2'>". $row['task_name'] . "</td>";
echo "<td align='center'><img src='data:image/jpeg;base64,".$before."'/></td>";
echo "<td align='center'>".$image."</td>"; // $image store your data
echo "<td rowspan='2' align='center'>". $row['ot_start']. "</td>";
echo "<td rowspan='2' align='center'>".$row['ot_end']. "</strong></td>";
echo "<td rowspan='2'><strong>". $row['report_status'] . "</strong></td>";
echo "<td rowspan='2' align='center'>";
echo "<a class='btn-view btn-primary btn-sm' href='../view_task/view_task.php?report_id=". $row['report_id'] ."' data-toggle='tooltip'>View</a>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'>". $row['time_photo_before'] . "</td>";
echo "<td align='center'>". $row['time_photo_after'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table><br>";
echo "</div>";
echo "<div>";
}
?>

Return only searched rows in table and hide other

If I write something in search box and press search , it should only return matched rows and hide other rows.
Here is my code, it works perfects only issue is it gives me searched record + all record list of table.
What can I do to show only searched data in table.?
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page=5;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $num_rec_per_page;
$result= mysql_query("SELECT * FROM products LIMIT $start_from, $num_rec_per_page");
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%".$term."%' or price LIKE '%".$term."' or details LIKE '%".$term."'";
$r_query = mysql_query($sql);
if($r_query>1)
{
while ($row = mysql_fetch_array($r_query)){
echo "<tr bgcolor='red'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
}
else{
echo "Nothing should be displayed";
}
}
?>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr class='danger'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
?>
</table>
Just keep single while loop and run the query and search query as shown below it will solve the issue:
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
};
$start_from = ($page - 1) * $num_rec_per_page;
$sql = "SELECT * FROM products LIMIT $start_from, $num_rec_per_page";
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1) {
while ($row = mysql_fetch_array($r_query)) {
echo "<tr bgcolor='red'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['details'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subcategory'] . "</td>";
echo "<td>" . $row['date_added'] . "</td>";
echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
} else {
echo "Nothing should be displayed";
}
?>
</table>

why mysql data has a single quote when displayed in PHP?

Im new to php and i have this code tested with XAMPP at backend
$name = addslashes ($_POST['name']);
$email = addslashes ($_POST['email']);
$paswd = addslashes ($_POST['paswd']);
$sql = "INSERT INTO webusers (username,email,paswd)VALUES('$name','$email','$paswd')";
and here's my code for displaying data
include_once('dbcon.php');
$db = mysql_select_db('test');
$sql = "SELECT * FROM webusers";
$result = mysql_query($sql);
$urow = mysql_num_rows($result);
echo"
<table border='1'>
<th>ID</th>
<th>USERNAME</th>
<th>EMAIL</th>
<th>PASSWORD</th>
";
if($result > 0){
while($urow = mysql_fetch_array($result)){
echo "<tr>";
echo" <td class='myinput'>'" .$urow['id']. "'</td>";
echo" <td class='myinput'>'" .$urow['username']. "'</td>";
echo" <td class='myinput'>'" .$urow['email']. "'</td>";
echo" <td class='myinput'>'" .$urow['paswd']. "'</td>";
echo "</tr>";
echo"</table>";
}
}else{
echo"No record";
}
?>
Those code works, except for the data which surprise me why it has a single quote when i show/display it on a table. though i input the data in html . and my magic_quote_gpc was off. is there something i missed or anything wrong with my code? or there is something with my database collation?
i also tried mysql_real_escape_string and mysql_escape_string, nothings change.
thanks for the help.
Otep
Because you put Single Quotes in the HTML ?
echo"
<table border='1'>
<tr>
<th>ID</th>
<th>USERNAME</th>
<th>EMAIL</th>
<th>PASSWORD</th>
</tr>
";
if($result > 0){
while($urow = mysql_fetch_array($result)){
echo "<tr>";
echo" <td class=\"myinput\">" .$urow['id']. "</td>";
echo" <td class=\"myinput\">" .$urow['username']. "</td>";
echo" <td class=\"myinput\">" .$urow['email']. "</td>";
echo" <td class=\"myinput\">" .$urow['paswd']. "</td>";
echo "</tr>";
echo"</table>";
}
}else{
echo"No record";
}
Try that, your code without Single quotes
You are printing a ' around your values in your HTML Markup
echo "<tr>";
echo" <td class='myinput'>" .$urow['id']. "</td>";
echo" <td class='myinput'>" .$urow['username']. "</td>";
echo" <td class='myinput'>" .$urow['email']. "</td>";
echo" <td class='myinput'>" .$urow['paswd']. "</td>";
echo "</tr>";
is what you really want to echo
Try this one
if($result > 0){
while($urow = mysql_fetch_array($result)){
echo "<tr>";
echo" <td class='myinput'>" .stripslashes($urow['id']). "</td>";
echo" <td class='myinput'>" .stripslashes($urow['username']). "</td>";
echo" <td class='myinput'>" .stripslashes($urow['email']). "</td>";
echo" <td class='myinput'>" .stripslashes($urow['paswd']). "</td>";
echo "</tr>";
echo"</table>";
}
}else{
echo"No record";
}

Deleting rows from table on web retrieved from database

I am having issue with deleting rows from a database that I echoed onto my website, I have used tick check boxes and when multiples are selected they should be deleted. But it's just NOT HAPPENING! Nothing is getting deleted from the database! please help!
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
if($result){
}
}
mysql_close();
?>
</form>
First off you can just implode() all the gathered ids from the form and from there build the query.
Sample code:
<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>
and while you still can, use mysqli or PDO, its free anyway

drop down menu not appearing in the following php code, but every other element is in the table

I'm using php to create a table from the data extracted from MySQL database. I'm using SELECT * From table_name and all data is appearing as I wish. But I want to create a Drop Down menu in the last column for that I'm using the function displayDMenu. But the drop down menu is still not appearing in the table. Can you please suggest what is causing the problem and solution? Or alternate solutions? Here's the code:
<div id="main">
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
echo "<table border='1'>
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding:3px'>" . $row['S_ID'] . "</td>";
echo "<td style='padding:3px'>" . $row['crnumber'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdTime'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdDate'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdMonth'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdYear'] . "</td>";
echo "<td style='padding:3px'>" . $row['DName'] . "</td>";
echo "<td 'style='padding:3px'>" . displayDMenu() . "</td>";
echo "</tr>";
}
echo "</table>";
function displayDMenu() {
$r = '';
$r .='<form method="post" action="ScdApproval.php">';
$r .='<select name="Status">';
$r .='<option value="approved" selected>Approve</option>';
$r .='<option value="disapproved">Disapprove</option>';
$r .='</select>';
$r .='</form>';
}
?>
</div> <!--main ends here -->
Not sure why you need the user function its not doing anything logical its just spitting back html which there is no reason not to put that html in the while loop.
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
?>
<style>#main table tr td{padding:3px;}</style>
<div id="main">
<table border="1">
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>
<?php
if(mysql_num_rows($result) > 0):
while($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row['S_ID']?></td>
<td><?php echo $row['crnumber']?></td>
<td><?php echo $row['ScdTime']?></td>
<td><?php echo $row['ScdDate']?></td>
<td><?php echo $row['ScdMonth']?></td>
<td><?php echo $row['ScdYear']?></td>
<td><?php echo $row['DName']?></td>
<td><form method="post" action="ScdApproval.php">
<select name="Status">
<option value="approved" selected>Approve</option>
<option value="disapproved">Disapprove</option>
</select>
</form>
</td>
</tr>
<?php
endwhile;
else: ?>
<tr>
<td rowspan="8">No results</td>
</tr>
<?php endif;?>
</table>
</div> <!--main ends here -->
Also:
Really you should also switch to using PDO or mysqli prepared querys for new code.
You need to return the value $r in the function displayDMenu()
ie
function displayDMenu()
{
.........
return $r;
}

Categories