get data from database with php and mysql (create report) - php

I have a create a report form which has 2 dropdown lists and table in above. I want to filter the data and make report with dropdown list options. The first dropdown list has all table names from database and second one has "item" column, and every table has same items values. Like when we make report from table 3 and item option 1. We should make report.
Here is my code for HTML:
<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>Report by Items</h5>
</div>
<div class="ibox-content">
<form method="post" class="form-horizontal">
<div class="form-group"><label class="col-sm-4 control-label"Stocks</label>
<div class="col-sm-4"><select class="form-control m-b" name="Stock">
<option></option>
<option>Stock</option>
<option>Stock1</option>
<option>Stock2</option>
</select>
</div>
</div>
<div class="form-group"><label class="col-sm-4 control-label">Items</label>
<div class="col-sm-4">
<select class="form-control m-b" name="Items">
<option></option>
<option>Item1</option>
<option>Item2</option>
<option>Item3</option>
<option>Item4</option>
</select>
</div>
</div>
</div>
<div class="col-lg-25">
<div class="ibox float-e-margins">
</div>
</div>
<div class="form-group">
<div class="col-sm-4 col-sm-offset-5">
<button class="btn btn-white" type="submit">Cancel</button>
<button class="btn btn-primary" type="submit" name=submit>Run Report</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
PHP and table code I try is:
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>No</th>
<th>Quantity</th>
<th>Date</th>
<th>Sold</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
$mysqli = new mysqli( 'localhost', 'user', 'pass', 'mis_db' );
if (mysqli_connect_error()) {
echo mysqli_connect_error();
exit();
}
if($_POST['Stock']=='Stock1')
{
$stck1 = 'Stock1';
}
if($_POST['Stock']=='Stock2')
{
$stck1 = 'Stock2';
}
if($_POST['Stock']=='Stock1')
{
$stck1 = 'Stock3';
}
if($_POST['Items']=='Item1')
{
$itm = 'Item1';
}
if($_POST['Items']=='Item2')
{
$itm = 'Item2';
}
if($_POST['Items']=='Item3')
{
$itm = 'Item3';
}
if (isset($_POST['submit'])) {
$query = 'SELECT * FROM .$stck1 where Items=.$itm';
$data = mysqli_query($mysqli, $query) ;
if (!$data) {
echo("Error description: " . mysqli_error($mysqli));
} else {
while ($row = mysqli_fetch_array($data)) {
echo "<tr>
<td>" . $row['No'] . "</td>
<td>" . $row['Qty'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['Sold'] . "</td>
<td>" . $row['Total'] . "</td>
</tr>";
}
}
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
I have doubt on MySQL query.

You have some syntax errors and your PHP can be simplified quite a bit.
As you can see I removed the if statements for each item in the dropdowns, not needed. You also had syntax errors in your $query, no . are required for concatenation so I've removed them. Finally, the $stck1 variable in the $query changed to simply $stck.
Try this:
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'mis_db');
if (mysqli_connect_error()) {
echo mysqli_connect_error();
exit();
}
$stck = $_POST['Stock'];
$itm = $_POST['Items'];
if (isset($_POST['submit'])) {
$query = "SELECT * FROM $stck WHERE Items = $itm";
$data = mysqli_query($mysqli, $query) ;
if (!$data) {
echo("Error description: " . mysqli_error($mysqli));
} else {
while ($row = mysqli_fetch_array($data)) {
echo "<tr>
<td>" . $row['No'] . "</td>
<td>" . $row['Qty'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['Sold'] . "</td>
<td>" . $row['Total'] . "</td>
</tr>";
}
}
}
?>

Related

PHP use MYSQL table result as parameter for another query

I have a table on my site that is pulled from MYSQL based on a search. I would like to be able to click a button added to the table which opens a modal with more data based on the row selected. In the below image I have a column labeled ID for the first table, which could be used for the = ? in my query, but I do not know how to set the value as a parameter first. Any thoughts on the best way to go about this?
I have $term = '%' . $_GET['itemID'] . '%'; added for the second query but need a way to bind ID 999999, 1000000, or 1000001 based on the corresponding view button. Thanks!
if(isset($_GET['term']))
{
$term = '%' . $_GET['term'] . '%';
$sql1 = "SELECT ID, Source, Contract_Number, Price_Effective, Price_Expiration, Min_Price, Average_Price
FROM Both_Search WHERE SearchName LIKE ? LIMIT 10";
$stmt = $conn->prepare($sql1);
$stmt->bind_param("s", $term);
$stmt->execute();
$result1 = $stmt->get_result();
while($row1 = $result1->fetch_assoc()) {
echo "<td>" . $row1["ID"] . "</td>"; <----would want this to be the parameter for next query
echo "<td>" . $row1["Source"] . "</td>";
echo "<td><a target='_blank' rel='noopener noreferrer' href='contracts.php?number=" . $row1["Contract_Number"] . "'>" . $row1["Contract_Number"] . "</td>";
echo "<td>" . $row1["Price_Effective"] . "</td>";
echo "<td>" . $row1["Price_Expiration"] . "</td>";
echo "<td>" . "$ " . $row1["Average_Price"] . "</td>";
echo "<td>" . "$ " . $row1["Min_Price"] . "</td>"; ?>
<td><button id="myBtn" type="button" class="btn btn-primary">View</button></td>
</tr><?php
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<div class="card shadow mb-4">
<div class="card-header py-3" style="background-color: rgb(90, 136, 255);">
<h6 class="m-0 font-weight-bold text-white">Tiers</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable2" width="100%" cellspacing="0">
<thead>
<tr>
<th>Price 1</th>
<th>Price 2</th>
<th>Price 3</th>
<th>Price 4</th>
<th>Price 5</th>
<th>Price 6</th>
<th>Price 7</th>
<th>Price 8</th>
<th>Price 9</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['itemID']))
{
$term = '%' . $_GET['itemID'] . '%';
$sql1 = "SELECT *
FROM Pricelist WHERE ID = ? LIMIT 10";
$stmt = $conn->prepare($sql1);
$stmt->bind_param("s", $term);
$stmt->execute();
$result1 = $stmt->get_result();
while($row1 = $result1->fetch_assoc()) {
echo "<td>" . "$ " . $row1["P_1"] . "</td>";
echo "<td>" . "$ " . $row1["P_2"] . "</td>";
echo "<td>" . "$ " . $row1["P_3"] . "</td>";
echo "<td>" . "$ " . $row1["P_4"] . "</td>";
echo "<td>" . "$ " . $row1["P_5"] . "</td>";
echo "<td>" . "$ " . $row1["P_6"] . "</td>";
echo "<td>" . "$ " . $row1["P_7"] . "</td>";
echo "<td>" . "$ " . $row1["P_8"] . "</td>";
echo "<td>" . "$ " . $row1["P_9"] . "</td>";
?>
</tr> <?php
}
}
$conn->close();
?>
</tbody>
<script>
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
You got the ID as $row1["ID"] and you can use that ID for Another Query.
// Replace this
<td><button id="myBtn" type="button" class="btn btn-primary">View</button></td>
// To this
<td>View</td>
And the button 'View' will redirect to YOUR_PAGE_VIEW.php with the Param Query ID = $row1["ID"].
Now you have the ID as variable $id for Another Query what you want in the YOU_PAGE_VIEW.php.
// In YOUR_PAGE_VIEW.php
$id = $_GET['id'];
Hope this help you,

How to put many tables on a page using a multi-tab system?

I'm on a project where I've to show 4 tables on the same page. I found this : https://www.codeandcourse.com/how-to-display-data-from-mysql-database-into-html-table-using-php/.
I've tried to put it on a system using cards with tabs (like that https://bootsnipp.com/snippets/z2Q7x), but it unfortunately doesn't work...
Actually, when I insert a table that show all the elements form my SQL database, I can't see the end of my page... For example, I inserted a footer and I can't see it if my table is on the page.
In fact, all the code that follow my table are missing on the final result.
Here is my actual work :
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<div class="col-md-12">
<div style="height: 2em"></div>
<div class="card">
<div class="card-header">
<h5>My tabs system</h5>
<ul class="nav nav-tabs card-header-tabs">
<li class="nav-item">
<a class="nav-link active" id="first-tab" data-toggle="tab" href="#first" role="tab" aria-controls="first" aria-selected="true">First</a>
</li>
<li class="nav-item">
<a class="nav-link"id="second-tab" data-toggle="tab" href="#second" role="tab" aria-controls="second" aria-selected="false">Second</a>
</li>
<li class="nav-item">
<a class="nav-link"id="third-tab" data-toggle="tab" href="#third" role="tab" aria-controls="third" aria-selected="false">Third</a>
</li>
<li class="nav-item">
<a class="nav-link"id="fourth-tab" data-toggle="tab" href="#fourth" role="tab" aria-controls="fourth" aria-selected="false">Fourth</a>
</li>
</ul>
</div>
<div class="card-body">
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="first" role="tabpanel" aria-labelledby="first-tab">
<table>
<tr>
<th>Id</th>
<th>Username</th>
<th>Password</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "company");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, password FROM login";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td><td>"
. $row["password"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</div>
<div class="tab-pane fade" id="second" role="tabpanel" aria-labelledby="second-tab">
<table>
<tr>
<th>Id</th>
<th>Username</th>
<th>Password</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "company");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, password FROM login";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td><td>"
. $row["password"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</div>
<div class="tab-pane fade" id="third" role="tabpanel" aria-labelledby="third-tab">
<table class="table table-striped">
<table>
<tr>
<th>Id</th>
<th>Username</th>
<th>Password</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "company");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, password FROM login";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td><td>"
. $row["password"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</div>
<div class="tab-pane fade" id="fourth" role="tabpanel" aria-labelledby="fourth-tab">
<table class="table table-striped">
<table>
<tr>
<th>Id</th>
<th>Username</th>
<th>Password</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "company");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, password FROM login";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td><td>"
. $row["password"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Thank you for your help !
Jojorealisateur

Select and Update form from table using php and mysqli

After writing a code to echo the result of my mysql to my database table, now am trying to get the selected row to be on my update page as as as show some information from the table and as well as being able to update the table at the same time
Am getting confused with all the online help i have actually read in other to make this work, but i believe i wrote the right code but might just be missing something in which i believe some on can help me with.
here is index.php that display the table from my database
<?php
//include auth.php file on all secure pages
require("../db.php");
session_start();
if(!isset($_SESSION["username"])){
header("Location: login");
exit(); }
?>
<?php require_once('header.php')?>
<div class="container content">
<table id="myTable" class="table table-striped" >
<thead>
<tr>
<th>ConsignmentNo</th>
<th>Origin</th>
<th>Destination</th>
<th>PickupDate</th>
<th>Status</th>
<th >Actions</th>
</tr>
</thead>
<tbody>
<?php
$result = mysqli_query($con,"SELECT * FROM consignment");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['consignmentno'] . "</td>";
echo "<td>" . $row['shipmentorigin'] . "</td>";
echo "<td>" . $row['shipmentdestination'] . "</td>";
echo "<td>" . $row['shipmentpickupdate'] . "</td>";
echo "<td>" . $row['shipmentstatus'] . "</td>";
echo "<td><a name='consignmentno' href='update.php?id=".$row['consignmentno']."'>Edit</a></td>";
echo "</tr>";
}
mysqli_close($con);
?>
</tbody>
</table>
</div>
</div>
<?php require_once('footer.php')?>
And here is the
update.phppage that process my request
<?php
require("../db.php");
$track = $_GET['consignmentno'];
$sql = "SELECT * FROM `consignment` WHERE consignmentno='$track'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$consignmentno = $row['consignmentno'];
$shippername = $row['shippername'];
$shipperphone = $row['shipperphone'];
$shipperaddress = $row['shipperaddress'];
$shipperemail = $row['shipperemail'];
$receivername = $row['receivername'];
$receiverphone = $row['receiverphone'];
$receiveraddress = $row['receiveraddress'];
$receiveremail = $row['receiveremail'];
$shipmenttype = $row['shipmenttype'];
$shipmentweight = $row['shipmentweight'];
$shipmentcourier = $row['shipmentcourier'];
$shipmentpackage = $row['shipmentpackage'];
$shipmentmode = $row['shipmentmode'];
$shipmentproduct = $row['shipmentproduct'];
$shipmentquantity = $row['shipmentquantity'];
$shipmentfrieght = $row['shipmentfrieght'];
$shipmentcarrier = $row['shipmentcarrier'];
$departeddate = $row['departeddate'];
$shipmentorigin = $row['shipmentorigin'];
$shipmentdestination = $row['shipmentdestination'];
$shipmentpickupdate = $row['shipmentpickupdate'];
$shipmentstatus = $row['shipmentstatus'];
$shipmentexpected = $row['shipmentexpected'];
$comment = $row['comment'];
}
} else {
echo "NO DETAILS FOR USER";
}
$con->close();
?>
<?php require_once('header.php')?>
<div class="container content">
<script type="text/javascript">
$(document).ready(function(){
txt=$("#UpdateStatus").val();
if(txt=='3')
{
$("#receive").slideDown("slow");
$("#UpdateReceivedBy").removeAttr("disabled");
}
else
{
$("#receive").slideUp("slow");
$('#UpdateReceivedBy').attr('disabled', 'true');
}
$("#UpdateStatus").change(function(){
txt=$("#UpdateStatus").val();
if(txt=='3')
{
$("#receive").slideDown("slow");
$("#UpdateReceivedBy").removeAttr("disabled");
}
else
{
$("#receive").slideUp("slow");
$('#UpdateReceivedBy').attr('disabled', 'true');
}
});
});
</script>
<h2 class="col-md-offset-5">Update Shipment</h2>
<div class="row">
<div class="table-responsive col-md-8 col-md-offset-2">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th><h4><?php echo $consignmentno ; ?></h4>
</th>
<th>
<h2>On Hold</h2>
</th>
</tr>
</thead>
but it is not echoing the $consignmentno cause if it can do that then every other things becomes easier
and here is an image that shows that my index page is working fine
So please help me check were i got it all wrong. Thanks
You must replace update.php?id= with update.php?consignmentno= on line 35 it will resolve your issue
Or use this code in index.php
<?php
//include auth.php file on all secure pages
require("../db.php");
session_start();
if(!isset($_SESSION["username"])){
header("Location: login");
exit(); }
?>
<?php require_once('header.php')?>
<div class="container content">
<table id="myTable" class="table table-striped" >
<thead>
<tr>
<th>ConsignmentNo</th>
<th>Origin</th>
<th>Destination</th>
<th>PickupDate</th>
<th>Status</th>
<th >Actions</th>
</tr>
</thead>
<tbody>
<?php
$result = mysqli_query($con,"SELECT * FROM consignment");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['consignmentno'] . "</td>";
echo "<td>" . $row['shipmentorigin'] . "</td>";
echo "<td>" . $row['shipmentdestination'] . "</td>";
echo "<td>" . $row['shipmentpickupdate'] . "</td>";
echo "<td>" . $row['shipmentstatus'] . "</td>";
echo "<td><a name='consignmentno' href='update.php?consignmentno=".$row['consignmentno']."'>Edit</a></td>";
echo "</tr>";
}
mysqli_close($con);
?>
</tbody>
</table>
</div>
</div>
<?php require_once('footer.php')?>

Assign value of a button for each row

I am trying to use php and I am really new to it.
I create one page that (with a php code) send the info to the database and one page that display the info from the databse.
My question is: Lets say I am going to input 10 info , but I am displaying just 5 of them, how can I display the rest of the info clicking to a button for each row?
Do I have to assign to the button a value? I cant understand how I can relate the button to the row.
Basically for each row I want to display the rest of the info of that specific row clicking on that specific button.
Any help will be appricciate. thanks
----- script that insert the info in the db-----
<?php
$name = mysqli_real_escape_string($link, $_POST['name']);
$mail = mysqli_real_escape_string($link, $_POST['mail']);
$number = mysqli_real_escape_string($link, $_POST['number']);
$device = mysqli_real_escape_string($link, $_POST['device']);
$model = mysqli_real_escape_string($link, $_POST['model']);
$problem = mysqli_real_escape_string($link, $_POST['problem']);
$sql = "INSERT INTO cj (name, mail, number, device, model, problem) VALUES ('$name', '$mail', '$number', '$device', '$model', '$problem')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
header("location:add-customer.php?message=A job and customer has been added to the database");
exit;
echo "You'll never see this";
?>
----------- page that shows the info ------
<?php
$sql = "SELECT * from "database name"";
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
echo "<form action='' method=post>";
echo "<tr class='info'>
<input type=hidden name=hidden value=" . $row['id'] . ">
<td>" . $row['id'] . "</td>
<td>" . $row['device'] . "</td>
<td>" . $row['model'] . "</td>
<td>" . $row['problem'] . "</td>
<td>
</td>
<td>
<a class='btn btn-primary btn-sm' data-toggle='modal' data-target='#myModal' value= " . $row['id'] . " > Info</a></form></td>
</tr>";
echo "</form>";
}
echo "
</tbody>
</table>";
?>
<div class="container">
<!-- Trigger the modal with a button -->
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Customer Information</h4>
</div>
<div class="modal-body">
<?php
include("../includes/connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
$sql = "SELECT name,mail,number from databasename WHERE **???**;
if (!$result = $link->query($sql)) {
die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
echo "
<th>" . $finfo->name . "</th>";
}
echo "
</tr>
</thead>
<tbody>";
while ($row = $result->fetch_assoc()) {
echo "<tr class='info'>
<td>" . $row['name'] . "</td>
<td>" . $row['mail'] . "</td>
<td>" . $row['number'] . "</td>
</tr>";
...
Connect to datatabase and get all 10 info with PHP. But show only 5 info. Put a button there, and use Javascript or Jquery to show other rows. (Onclick event)
I found a solution using ajax:
here is the code: (an ajax call to a third php page which fixed my problem)
<script>
$(document).ready(function(){
$('.get_info').click(function(){
var job_id = $(this).parent().siblings('input[name=hidden]').val();
$.ajax({
url: 'THIRDPAGE.php',
data: 'job_id=' + job_id,
type: 'POST',
success: function(data){
// console.log(data);
$('.info_data').html(data);
}
});
});
});
</script>
For the oone that is getting here just comment and I can explain better what I did.

Delete Row Button for MySQL and 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>';
?>

Categories