Delete query not working to delete stock - php

i am implementing clothing shopping website in which admin manages stock for example add, update, delete stock. i am having problem with my delete query. stock is displayed in table and every row has its own delete button. when i click on delete button it always takes last row id and delete that and not that row which i want. Query is taking always last row id.
CODE:
<form action="" method="post" enctype="multipart/form-data" name="deleting" >
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query ($sql);
while ($result=mysqli_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $result['id'];?></td>
<td><?php echo $result['brand_name'];?></td>
<td><?php echo $result['price'];?></td>
<td><?php echo $result['gender_name'];?></td>
<td><?php echo $result['category_name'];?></td>
<td><?php echo $result['material_name'];?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description'];?></td>
<td><?php echo $result['dress_quantity'];?></td>
<td><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></td>
</tr>
<?php
}
?>
</table>
</form>
<?php
if (isset($_POST['delete'])) {
$id=$_POST['ID']; //problem is here: it always takes last row id
$link=mysqli_connect("localhost","root","") or die("Cannot Connect to the database!");
mysqli_select_db("login",$link) or die ("Cannot select the database!");
$query="DELETE FROM add_stock WHERE id='".$id."'";
$result=mysqli_query($query,$link) or die(mysqli_error($link));
if($result)
{
echo '<script>confirm("Are you sure want to delete this record?")</script>';
echo '<script>alert("Record ".$id." removed successfully!")</script>';
}
else
{
die ("An unexpected error occured while <b>deleting</b> the record, Please try again!");
}
}
?>

I would rather suggest not to use <form> just for deleting purpose. You can use <a> tag to redirect it to other page for deleting purpose.
Here is my code.
index.php
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query($sql);
while ($result = mysqli_fetch_array($rs_result)) {?>
<tr>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['brand_name']; ?></td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['gender_name']; ?></td>
<td><?php echo $result['category_name']; ?></td>
<td><?php echo $result['material_name']; ?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description']; ?></td>
<td><?php echo $result['dress_quantity']; ?></td>
<td>
<a href="deleteStock.php?id=<?php echo $result['id'];?>">
<input type="button" value="Delete" >
</a>
</td>
</tr>
<?php }?>
</table>
<?php
if(isset($_GET['delete'])){
if($_GET['delete'] == "success"){
echo '<script>alert("Record removed successfully!")</script>';
}
if($_GET['delete'] == "fail"){
echo '<script>alert("An unexpected error occured while <b>deleting</b> the record, Please try again!")</script>';
}
}
?>
<script>
$(document).on('click', "#myTable a", function(e) {
if(confirm("Are you sure want to delete this record?")) {
return true;
} else {
return false;
}
});
</script>
deleteStock.php
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$link = mysqli_connect("localhost", "root", "") or die("Cannot Connect to the database!");
mysqli_select_db("login", $link) or die("Cannot select the database!");
$query = "DELETE FROM add_stock WHERE id = $id";
$result = mysqli_query($query, $link) or die(mysqli_error($link));
if($result){
header("location:index.php?delete=success");
} else {
header("location:index.php?delete=fail");
}
}?>
[Important: And, still you have not created separate file for DB Connection, which I have already mentioned in my answer of your question modal popup keep populating only first item data on all item buttons 1 Week before. It implies, you don't learn from your mistake or you don't need any suggestions.]

Your script taking last row in post always because you have single form for all your data and your last value will be overwrite all previous data. Intead of it remove form from out side of table add add it to td where you have specify hidden field and delete button. Like below:
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query ($sql);
while ($result=mysqli_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $result['id'];?></td>
<td><?php echo $result['brand_name'];?></td>
<td><?php echo $result['price'];?></td>
<td><?php echo $result['gender_name'];?></td>
<td><?php echo $result['category_name'];?></td>
<td><?php echo $result['material_name'];?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description'];?></td>
<td><?php echo $result['dress_quantity'];?></td>
<td><form action="" method="post" name="deleting" ><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></form></td>
</tr>
<?php
}
?>
</table>
Also Remove enctype="multipart/form-data" from form its needed only if you have to upload file

Related

Datatables console error jQuery.Deferred exception: Cannot set property '_DT_CellIndex'

my datatables didn't work and i get console error Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined but when i delete the query inside tbody its working fine.
<table class="table table-bordered table-striped table-fixed text-center" id="myTable">
<thead>
<tr>
<th>No</th>
<th>ID</th>
<th>Nama</th>
<th>Golongan</th>
<th>Nilai Output</th>
<th>Penilaian Atasan</th>
<th>Nilai Learning</th>
<th>Nilai Kedisiplinan</th>
<th>Nilai 5R</th>
<th>Hasil</th>
<th>Tanggal</th>
</tr>
</thead>
<!-- query -->
<?php
$no = 0;
$query = "SELECT * FROM user
where id= $_SESSION[id]";
$query = "SELECT user.id,user.nama,golongan,nilai_output,nilai_atasan,nilai_learning,nilai_kedisiplinan,nilai_5r,overall,tanggal
FROM tkaryawan
JOIN user
ON user.id=tkaryawan.id
where user.id= $_SESSION[id]
ORDER BY tanggal DESC";
$result = mysqli_query($koneksi, $query);
if(!$result){
die ("Query Error: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
}
while($data = mysqli_fetch_assoc($result))
{
$no++;
?>
<tbody>
<tr>
<td><?php echo $no;?></td>
<td><?php echo $data['id'];?></td>
<td class="text-capitalize"><?php echo $data['nama'];?></td>
<td><?php echo $data['golongan'];?></td>
<td><?php echo $data['nilai_output'];?></td>
<td><?php echo $data['nilai_atasan'];?></td>
<td><?php echo $data['nilai_learning'];?></td>
<td><?php echo $data['nilai_kedisiplinan'];?></td>
<td><?php echo $data['nilai_5r'];?></td>
<td class="font-weight-bold text-danger"><?php echo $data['overall'];?></td>
<td class="text-secondary"><?php echo $data['tanggal'];?></td>
<tr>
</tbody>
<?php
}
// free the memory
mysqli_free_result($result);
// close conection
mysqli_close($koneksi);
?>
</table>
appreciate any help you can provide.
EDITED
<?php
$stmt = $mysqli->prepare("SELECT id,nama FROM user WHERE user.id= $_SESSION[id]");
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();
if(!$row){
mysqli_query($link, $sql);
}
else
{
echo "<h3 class='text-uppercase'>HASIL KINERJA </br> <p class='font-weight-bold text-danger'> $row[nama] </p> </h3>";
}
?>
You put your while loop above tbody which will loop the tbody as well. You need to move the while and its closing tag on <tr>. Also try to change the closing tr tag on tbody to be </tr>.
<table class="table table-bordered table-striped table-fixed text-center" id="myTable">
<thead>
<tr>
<th>No</th>
<th>ID</th>
<th>Nama</th>
<th>Golongan</th>
<th>Nilai Output</th>
<th>Penilaian Atasan</th>
<th>Nilai Learning</th>
<th>Nilai Kedisiplinan</th>
<th>Nilai 5R</th>
<th>Hasil</th>
<th>Tanggal</th>
</tr>
</thead>
<!-- query -->
<?php
$no = 0;
$query = "SELECT * FROM user
where id= $_SESSION[id]";
$query = "SELECT user.id,user.nama,golongan,nilai_output,nilai_atasan,nilai_learning,nilai_kedisiplinan,nilai_5r,overall,tanggal
FROM tkaryawan
JOIN user
ON user.id=tkaryawan.id
where user.id= $_SESSION[id]
ORDER BY tanggal DESC";
$result = mysqli_query($koneksi, $query);
if(!$result){
die ("Query Error: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
}
?>
<tbody>
<?php
while($data = mysqli_fetch_assoc($result))
{
$no++;
?>
<tr>
<td><?php echo $no;?></td>
<td><?php echo $data['id'];?></td>
<td class="text-capitalize"><?php echo $data['nama'];?></td>
<td><?php echo $data['golongan'];?></td>
<td><?php echo $data['nilai_output'];?></td>
<td><?php echo $data['nilai_atasan'];?></td>
<td><?php echo $data['nilai_learning'];?></td>
<td><?php echo $data['nilai_kedisiplinan'];?></td>
<td><?php echo $data['nilai_5r'];?></td>
<td class="font-weight-bold text-danger"><?php echo $data['overall'];?></td>
<td class="text-secondary"><?php echo $data['tanggal'];?></td>
</tr>
<?php } ?>
</tbody>
<?php
// free the memory
mysqli_free_result($result);
// close conection
mysqli_close($koneksi);
?>
</table>

How can I make the data that is displayed on the table change with value chosen in a select box?

Based on the user that is chosen within the combo box, I want the table that is displaying user data from the database to only show the data corresponding to the user selected in the combo box.
I mainly tried using an array to store values but I couldn't get that working.
Combo Box that displays the name to pick
<select>
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded">Find</button>
</select>
</form>
Table that shows the data from the database.
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
//Fetch Data form database
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
I'm wondering if by using the form and doing a function that on pressing the Find button it looks up the user and displays only it's data. Thanks
Check my code here, there are some thing you must add, like a WHERE statement to your query, when fetching data to only show results with the selected name in the form
<!-- Create a form with a select for all the names -->
<form method="POST" action="">
<select name="name">
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded" name="find_info">Find</button>
</select>
</form>
<?php
if(isset($_POST['find_info'])){ //If find button is pressed, show this:
?>
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
$nameSelected = strip_tags(mysqli_real_escape_string(htmlspecialchars($_POST['name'])));
// Use WHERE in your mysqli query to fetch data where name in database is equal to $nameSelected
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>

Column name field showing multiple time in php?

I am searching records and records are displaying from database, but column name is repeating.Please check below images.
<table border="1" align="center">
<thead>
<tr>
<th>User id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM newrecords_1 WHERE CONCAT( First_name, ' ',Last_name ) LIKE '%$name%' ORDER BY `First_name` ASC";
$result = $conn->query($query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>
</tbody>
</table>
<?php
}
} else {
echo "0 results";
}
What i am getting
What i need
after added html code on header getting output
Put your table code outside of the loop like below :-
<table border="1" align="center">
<thead>
<tr>
<th>User id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM newrecords_1 WHERE CONCAT( First_name, ' ',Last_name ) LIKE '%$name%' ORDER BY `First_name` ASC";
$result = $conn->query($query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>
<?php } } else { echo "0 results";}?>
</tbody>
</table>
You are getting header part after every fetching because you are using header part also in loop. Place header part above loop and keep only
below code in loop.
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>

how do i create html table like on the picture

i have a 3 table and im doing inner join in the output below
how can i achive something like this
so far i already have code for expanding the row. the real problem here is how do i get all the signatoryname for each tracknum. im using php and html
this is my code
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5"><p><?php echo $r['signatoryname'] ?></p>
</td></tr>
<?php endwhile; ?>
</table>
for the table to expand
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$("td[colspan=5]").find("p").hide();
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
if ( $target.closest("td").attr("colspan") > 1 ) {
$target.slideUp();
} else {
$target.closest("tr").next().find("p").slideToggle();
}
});
});
});//]]>
</script>
this is the output of the code
i need to group the data below by tracknum but i need the signatoryname
i want the html row to be expandable and list the signatoryname of that tracknum bellow it. thanks.
update: so far this is my code
UPDATE: below is the correct code:
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
// execute the stored procedure
$sql = 'CALL sp_trasactionsignatory()';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $r['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Problem:
How do I get all the signatoryname for each tracknum?
Solution:
Your very first initial query would be like this,
(Since you didn't provide the table name, change the table name from the below queries)
$q = $connection->query("SELECT tracknum, doctitle, doctype, datefilled FROM tablename GROUP BY tracknum");
Then comes to your table,
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch_assoc()): ?>
<tr>
<td><?php echo $r['tracknum']; ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
$result_set = $connection->query("SELECT signatoryname FROM tablename WHERE tracknum = {$r['tracknum']}");
while ($row = $result_set->fetch_assoc()){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Don't forget to change the tablename in both the queries.
Edited:
If you using PDO extensions to execute your queries then you can do something like this,
<?php
$_tempp1 = $r['tracknum'];
$stmt = $connection->prepare("SELECT signatoryname FROM tablename WHERE tracknum = :tracknum");
$stmt->bindParam(':tracknum', $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>

status change on button click php mysql PDO

I am trying to change the package status as received in MySQL table, i guess the action is not performing well, can someone please spot the error, i am pasting the code below.
When i am putting the action code inside the while loop, it changes the status to Received for all the records. But when i am putting it outside the while loop, nothing happens.
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?>
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<form action='#' method='post' name='updatestatus'>
<input type='submit' name='submit' value='Mark As Recieved' />
</form>
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
while($q->fetch()) {
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
$q = $conn->prepare($sql);
$q->execute(array($status,$custemail));
header('Location:cust_orders.php');
}
}
?>
</tbody>
</table>
Please find the corrected code with the proper output.
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(1, $pid);
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?>
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<form action='#' method='post' name='updatestatus'>
<input type='hidden' name='pid' value='$pid'>
<input class='btn btn-inverse' type='submit' name='submit' value='Mark As Recieved'><i class='icon-refresh icon-white'></i></input>
</form>
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE package_id=?";
$q = $conn->prepare($sql);
$q->execute(array($status,$_POST['pid']));
header('Location:cust_orders.php');
}
?>
</tbody>
</table>
You need to define email, try this:
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?>
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<form action='#' method='post' name='updatestatus'>
<input type='submit' name='submit' value='Mark As Recieved' />
<input type='hidden' name='cust_email' value='<?php echo $custemail; ?>' />
</form>
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
while($q->fetch()) {
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
$q = $conn->prepare($sql);
$q->execute(array($status,$_POST['cust_email']));
header('Location:cust_orders.php');
}
}
?>
</tbody>
If you want to do multiple update by one submit. For this you need create one form for all items, like this:
<form action='#' method='post' name='updatestatus'>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<input type='checkbox' name='cust_email[]' value='<?php echo $custemail; ?>' />
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<tr class="odd gradeX">
<td colspan="7">
<input type='submit' name='submit' value='Mark As Recieved' />
</td>
<tr>
</tbody></table></form>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
while($q->fetch()) {
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
$q = $conn->prepare($sql);
foreach($_POST['cust_email'] as $cust_email)
$q->execute(array($status,$cust_email));
header('Location:cust_orders.php');
}
}
?>

Categories