Delete Row Button for MySQL and PHP - php

I have an admin panel where administrators can view all the data in a table, however I would like them to have a small delete button next to each row. Each row can be defined by an ID, however I am not sure about the code behind it.
<!-- Requests -->
<div class="panel panel-<?php echo $PColor; ?>">
<table class="table table-striped table-requests">
<thead class="table-requests">
<tr>
<th>Req ID</th>
<th><?php echo "Song Name"; ?></th>
<th><?php echo "Requested By"; ?></th>
<th><?php echo "Comments (If any)"; ?></th>
<th><?php echo "Time"; ?></th>
</tr>
</thead>
<tbody>
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");
// Print Output
foreach($SelectRequests as $PrintRequests)
{
echo
"
<tr>
<td><b>" . $PrintRequests['ID'] . "</b></td>
<td>" . $PrintRequests['song'] . "</td>
<td>" . $PrintRequests['name'] . "</td>
<td>" . $PrintRequests['dedicated'] . "</td>
<td>" . $PrintRequests['time'] . "</td>
";
}
?>
</tbody>
</table>
</div>
</div>
Is there any way to have the delete button to the right of each displayed row, and make it functional?
Thank you in advance!

<!-- Lets call this file index.php -->
<!-- Requests -->
<div class="panel panel-<?php echo $PColor; ?>">
<form action='deleteRecord.php' method='GET'>
<table class="table table-striped table-requests">
<thead class="table-requests">
<tr>
<th>Req ID</th>
<th><?php echo "Song Name"; ?></th>
<th><?php echo "Requested By"; ?></th>
<th><?php echo "Comments (If any)"; ?></th>
<th><?php echo "Time"; ?></th>
<!-- Add this for the table header -->
<th><?php echo "Delete"; ?></th>
</tr>
</thead>
<tbody>
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");
// Print Output
foreach($SelectRequests as $PrintRequests){
echo
"<tr>
<td><b>" . $PrintRequests['ID'] . "</b></td>
<td>" . $PrintRequests['song'] . "</td>
<td>" . $PrintRequests['name'] . "</td>
<td>" . $PrintRequests['dedicated'] . "</td>
<td>" . $PrintRequests['time'] . "</td>
<td><input type='checkbox' value ='" . $PrintRequests['ID'] . "' name='removal[]'></td>
</tr>";
}
?>
</tbody>
</table>
<input type="submit" value="Submit">
</form>
</div>
deleteRecord.php
<?php
//Assume you can connect to DB
$removals = $_GET['removal'];
if(!empty($removals)){
if(count($removals) > 1){
$r = implode(', ', $removals);
$query="DELETE from requests where ID IN (". $r . ")";
} else {
$query="DELETE from requests where ID = ". $removals[0];
}
//Commit to db
} else {
echo "Nothing Selected";
//Handle an error if needed
}
//redirect
?>

try something like that :
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");
// Print Output
foreach($SelectRequests as $PrintRequests)
{
echo
"
<tr>
<td><b>" . $PrintRequests['ID'] . "</b></td>
<td>" . $PrintRequests['song'] . "</td>
<td>" . $PrintRequests['name'] . "</td>
<td>" . $PrintRequests['dedicated'] . "</td>
<td>" . $PrintRequests['time'] . "</td>
<td> <a href='delete.php?pid=" . $PrintRequests['ID'] . "'> Delete</a> </td>;
";
}
?>
In delete.php
<?php
// Connexion to database..
$Query="delete from requests where id=".$_REQUEST['pid'];
if(!mysqli_query($Conection,$Query)) {echo "<i>Error !</i>";}
echo '<script language="javascript"> document.location="Your_previous_page.php";</script>';
?>

Related

Why is formatting broken when using bootstrap in PHP?

I want to generate Bootstrap 5 table .table-striped using data from MySQL, table: [users]
The table in plain HTML looks fine, but as soon as I generate it using PHP all styling disappears
PHP Code:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
echo "<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "</tr>";
echo " </tbody>";
}
echo "</table>";
mysqli_close($link);
?>
and I have <div class='table-responsive'> before <?php and </div> after ?>
I would be glad if you could explain me where I am making a mistake and I would like the table to have the same styling as in pure HTML
EDIT:
rendered HTML
<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead><tbody><tr><td>39</td><td></td><td>julia</td><td>2021-12-20 00:42:14</td></tr> </tbody><tbody><tr><td>40</td><td>test</td><td>user</td><td>2021-12-20 02:35:37</td></tr> </tbody><tbody><tr><td>41</td><td>testt</td><td>user</td><td>2021-12-20 17:22:43</td></tr> </tbody></table></div>
You are building a new tbody for every row. You should move the tbody creation outside the while loop. You also only need 1 echo, rather than echoing every line. I would do:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
$output = "<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th style='background-color:#ad8c70'>#</th>
<th style='background-color:#ad8c70'>Username</th>
<th style='background-color:#ad8c70'>Rank</th>
<th style='background-color:#ad8c70'>Created at</th>
</tr>
</thead>
<tbdoy>";
while($row = mysqli_fetch_array($result))
{
$output .= "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['username'] . "</td>
<td>" . $row['rank'] . "</td>
<td>" . $row['created_at'] . "</td>
</tr>";
}
mysqli_close($link);
$output .= '</tbody></table>';
echo $output;
?>

Display table vertically

I want to display table vertically. Now, It's displaying horizontally. Each
and every field is displaying. How can I display table vertically?
And one more thing I want to add one more function to the button. Current
date added into database when button
submit is clicked. For now this code is making my status 0 and not updating
date in database.
<html>
<head>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-
1252" />
<link rel="stylesheet" type="text/css" href="style/style.css" />
<style>
<table>
{
border-style:solid;
border-width:2px;
border-color:pink;
}
</style>
</head>
<?php
include 'inc/head.php';
include 'connection.php';
$id = $_GET['id'];
$sql = "SELECT * FROM user WHERE id='$id'";
$query = mysqli_query($conn, $sql);
echo "<table border='6'>
<tr>
<th>Application Type</th> <br>
<th>First name</th><br>
<th>Sur name</th><br>
<th>title</th>
<th>Date of Birth</th>
<th>Gender</th>
<th>Nationality</th>
<th>Country of Birth</th>
<th>Applicant's Location</th>
<th>alias</th>
<th>Criminal </th>
<th>Country of passport</th>
<th>Passport number</th>
<th>Passport issue date </th>
<th>Passport expiry date</th>
<th>Address </th>
<th>City</th>
<th>State</th>
<th>Postal Code</th>
<th>Country</th>
<th>Email Address</th>
<th>Home Number</th>
<th>Business Number</th>
<th>mobile</th>
</tr>";
while($row = mysqli_fetch_assoc($query)) {
echo "<tr>";
echo " <td>" . $row['applicationtype'] . "</td>";
echo " <td>" . $row['firstname'] . "</td>";
echo " <td>" . $row['lastname'] . "</td>";
echo " <td>" . $row['title1'] . "</td>";
echo " <td>" . $row['dob'] . "</td>";
echo " <td>" . $row['gender'] . "</td>";
echo " <td>" . $row['nationality'] . "</td>";
echo " <td>" . $row['countryofbirth'] . "</td>";
echo " <td>" . $row['applicantlocation'] . "</td>";
echo " <td>" . $row['alias'] . "</td>";
echo " <td>" . $row['criminal'] . "</td>";
echo " <td>" . $row['countryofpassport'] . "</td>";
echo " <td>" . $row['passportnumber'] . "</td>";
echo " <td>" . $row['pid'] . "</td>";
echo " <td>" . $row['ped'] . "</td>";
echo " <td>" . $row['address'] . "</td>";
echo " <td>" . $row['city'] . "</td>";
echo " <td>" . $row['state'] . "</td>";
echo " <td>" . $row['postalcode'] . "</td>";
echo " <td>" . $row['country'] . "</td>";
echo " <td>" . $row['email'] . "</td>";
echo " <td>" . $row['homephone'] . "</td>";
echo " <td>" . $row['businessphone'] . "</td>";
echo " <td>" . $row['mobile'] . "</td>";
echo "</tr>";
}
echo "</table><br><br>";
if(isset($_POST['submit'])) {
echo $radio = $_POST['radio'];
$date_clicked = date('Y-m-d H:i:s');
$sql = "UPDATE user SET visastatus='$radio' AND currentdate='$date_clicked' WHERE id='$id'";
$query = mysqli_query($conn, $sql);
if($query) {
echo "<h4 style='color:green'>Action Performed Successfully....</h4>";
} else {
echo "<h4 style='color:red'>Failed.</h4>";
}
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6" style="margin-top:
8px;">
<label class="checkbox-inline tourist-rad">
<input type="radio" name="radio" id="success"
value="Successfull">Successfull
</label>
<label class="checkbox-inline tourist-rad">
<input type="radio" name="radio" id="decline" value="Declined">Declined
</label>
</div>
<div class="form-group">
<button type="submit" name="submit" value="submit" class="btn btn-
primary" style="float: right;">Update Menu Item</button>
</div>
</body>
</html>
How can I display the table vertically?
Put each heading in a new <tr>. Remember, <tr> stands for table row, while <td> is for each of the elements in that row. This will require some modification of your php code so that you can essentially print a new row, and each td within it.
Edit: To display it vertically as you like, you'd need to modify your existing while loop.
//Print the row
echo "<tr>";
//Print the row heading
<th>Application Type</th>
while($row = mysqli_fetch_assoc($query)) {
//Print each matching data type inside
echo " <td>" . $row['applicationtype'] . "</td>";
}
//Close the row
echo "</tr>";
You'll need to do this functionality for each of the headings you have. You could also use arrays along with nested loops to print all the data cleanly and easily without excess code.
If by "vertically" you mean you want to have 2 columns and 24 rows instead of 24 columns and 2 rows, this will do it for you:
echo "<table border='6'>";
while($row = mysqli_fetch_assoc($query)) {
echo "<tr><th>Application Type</th><td>{$row['applicationtype']}</td></tr>";
echo "<tr><th>First Name</th><td>{$row['firstname']}</td></tr>";
echo "<tr><th>Surname</th><td>{$row['lastname']}</td></tr>";
echo "<tr><th>Title</th><td>{$row['title1']}</td></tr>";
echo "<tr><th>Date of Birth</th><td>{$row['dob']}</td></tr>";
echo "<tr><th>Gender</th><td>{$row['gender']}</td></tr>";
echo "<tr><th>Nationality</th><td>{$row['nationality']}</td></tr>";
echo "<tr><th>Country of Birth</th><td>{$row['countryofbirth']}</td></tr>";
echo "<tr><th>Applicant's Location</th><td>{$row['applicantlocation']}</td></tr>";
echo "<tr><th>Alias</th><td>{$row['alias']}</td></tr>";
echo "<tr><th>Criminal</th><td>{$row['criminal']}</td></tr>";
echo "<tr><th>Country of Passport</th><td>{$row['countryofpassport']}</td></tr>";
echo "<tr><th>Passport Number</th><td>{$row['passportnumber']}</td></tr>";
echo "<tr><th>Passport Issue Date</th><td>{$row['pid']}</td></tr>";
echo "<tr><th>Passport Expiry Date</th><td>{$row['ped']}</td></tr>";
echo "<tr><th>Address</th><td>{$row['address']}</td></tr>";
echo "<tr><th>City</th><td>{$row['city']}</td></tr>";
echo "<tr><th>State</th><td>{$row['state']}</td></tr>";
echo "<tr><th>Postal Code</th><td>{$row['postalcode']}</td></tr>";
echo "<tr><th>Country</th><td>{$row['country']}</td></tr>";
echo "<tr><th>Email Address</th><td>{$row['email']}</td></tr>";
echo "<tr><th>Home Number</th><td>{$row['homephone']}</td></tr>";
echo "<tr><th>Business Number</th><td>{$row['businessphone']}</td></tr>";
echo "<tr><th>Mobile</th><td>{$row['mobile']}</td></tr>";
}
echo "</table>";
Additionally, if you are going to be handling such sensitive personal data, you MUST improve your query security starting with mysqli prepared statements using placeholders.

PHP if statement in html-table outputs error

I am new to HTML5 and PHP, I am trying to output a specific value in table data, If the database-retrieved-value is per condition.
My code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
. "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Error output
Parse error: syntax error, unexpected T_IF in
/test/composition/login/portal/portal.php
on line 204
What is the right way to solve this?
EDIT
my current code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>Naam Client</th>
<th>Laatste Update</th>
<th style="margin-left: 40%; padding-left: 0%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable" style="font-size: 11pt;">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, naam, datum FROM clients";
$result = mysql_query($query);
$query2 = "SELECT status FROM clients";
$result2 = mysql_query($query2);
if (!empty ($result2)) {
while ($row2 = mysql_fetch_assoc($result2)) {
echo $row2['status'] . "<br />";
}
}
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['naam'] . "</td>
<td>" . $row['datum'] . "</td>
<td style='padding-left: 30%;'>";
if ($results2 > 60 && $results2 < 70) {
echo "red";
} elseif ($results2 > 50 && $results2 < 60) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Output the right data. but partly outside and partly inside the table.
You will have to remove the if statement out of the echo to get rid of the error Try this:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60) {
echo "red";
} elseif ($status_ > 50) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
You can't have an if statement (or any other statement, for that matter) in the middle of another statement like echo. If you want to concatenate different strings depending on a variable, you can use the conditional (AKA "ternary") operator.
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
$status_ > 60 ? "red" : ($status_ > 50 ? "yellow" : "green" )
. "</td>
</tr>";
Try:
$status = "green";
if ($status > 50)
{
$status="yellow";
}
elseif($status>60)
{
$status="red";
}
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .$status. "</td>
</tr>";
You can't append to a string a conditional statement, assign to a variable first for example (like I posted)
This part isn't at the right place:
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
should be:
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60){
echo "red";
} elseif ($status_ > 50){
echo "yellow";
} else{
echo "green";
}
echo "</td></tr>";
Surely status_ would not come back with a number, but an array.
$status_ = mysql_query($status);
Without knowing what data is coming back, it is difficult to help.
mysql_query

For loop for the first row, the button cant work

This is the table i created , so when i press the Edit button it should bring me to another page to edit. For the first edit button on the first row it does not work however for the rest of the edit button on the second row onward it works. Please help. Thank you !
<table border='1'>
<tr>
<th width='120'>Production ID</th>
<th width='120'>Description</th>
<th width='120'>BOM</th>
</tr>
<?php
for ($i = 0; $i < $num; $i++) {
$fetch = mysqli_fetch_row($select);
echo "<tr>
<td>" . $fetch[0] . "</td>
<td>" . $fetch[1] . "</td>
<td>" . $fetch[2] . "</td>
<td><form action='AddProductMainEdit.php?prodid' method='GET'><input type='hidden' value='".$fetch[0]."' name='prodid'><input type='submit' id='edit' value='Edit'></form></td>
</tr>";
}
?>
</table>
Try this:
<table border='1'>
<tr>
<th width='120'>Production ID</th>
<th width='120'>Description</th>
<th width='120'>BOM</th>
</tr>
<?php
while ($fetch=mysqli_fetch_row($select)) {
echo "<tr>
<td>" . $fetch[0] . "</td>
<td>" . $fetch[1] . "</td>
<td>" . $fetch[2] . "</td>
<td><form action='AddProductMainEdit.php' method='GET'><input type='hidden' value='".$fetch[0]."' name='prodid'><input type='submit' id='edit' value='Edit'></form></td>
</tr>";
}
?>
</table>
You don't need to add the variable prodid to the url, the get method add all the variables to the url.

making hyperlink of a value stored in table column

How to make the values inserted in table column($row['survey_name']) a hyperlink. this is my code which I have tried in php:
while($row=mysql_fetch_array($result1))
{
$content .= "
<tr id=\"special\" style=\"background:;\">
<td>". $row['survey_name'] . "</td>
<td>" . $row['date'] . "</td>
</tr>
";
?>
<script
$('#special').onclick(function(){window="http://urllinkto.com/x/y/z";})
</script>
<?php }//end of while
if ($content) {
// See note on the "quote switch" here
echo '
<table border= "5" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td><strong>Survey Name</strong></td>
<td><strong>Date Created</strong></td>
/tr>' .
$content . '
</table> ';
Chage
<td>". $row['survey_name'] . "</td>
to:
<td><a href='URL HERE'>". $row['survey_name'] . "</a></td>
Try to declare variables first.
while($row=mysql_fetch_array($result1)){
$surveyname = $row['survey_name']
$date = $row['date']
$content .= "
<tr id=\'special\' style=\"background:;\">
<td><a href='#'>echo $surveyname</a> </td>
<td><a href='#'>echo $date</a></td>
</tr>
";
}

Categories