status change on button click php mysql PDO - php

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');
}
}
?>

Related

instant delete when the button is cliked

im trying to delete from a datatable when i click the remove button the data will be removed for a quick action in the admin side but my code dose not work i tried to fix it but i don't see any problem in the code here is my code
<div class="table-responsive">
<table id="datas" class="table table-striped table-bordered fixed" style="width:100%">
<thead style="color:black;" >
<th>id</th>
<th>Company Name</th>
<th>Product Name</th>
<th>Weight</th>
<th>Price Per Gram</th>
<th>Quantity</th>
<th>Type</th>
<th>Category</th>
<th>Product Price</th>
<th>Image</th>
<th>Total Price</th>
<th class="text-center">Actions</th>
</thead>
<?php
$get = mysqli_query($conn,"SELECT * FROM stock;");
?>
<tbody>
<?php
while ($row=mysqli_fetch_array($get)) {
$id=$row['id'];
$company=$row['company_name'];
$name=$row['product_name'];
$weight=$row['weight'];
$price_per_gram=$row['price_per_gram'];
$quantity=$row['quantity'];
$type=$row['type'];
$category=$row['category'];
$price=$row['product_price'];
$img=$row['img'];
$total=$row['total_price'];
?>
<tr>
<td ><?php echo $id;?></td>
<td><?php echo $company;?></td>
<td><?php echo $name;?></td>
<td><?php echo $weight;?> g</td>
<td><?php echo $price_per_gram;?> $</td>
<td><?php echo $quantity;?></td>
<td><?php echo $type;?></td>
<td><?php echo $category;?></td>
<td><?php echo $price;?></td>
<td>
<img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
</td>
<td><?php echo $total;?></td>
<td style="width: 20px"> Edit
<button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
if (isset($_POST['delete'])) {
$delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." ");
header("location:viewstcok.php");
} ?>
i give the delete button a value when clicked so it will delete with that value but it did not seems to work can any one help me
you have to placed table inside <form></form> tag
<div class="table-responsive">
<form>
<table id="datas" class="table table-striped table-bordered fixed" style="width:100%">
<thead style="color:black;" >
<th>id</th>
<th>Company Name</th>
<th>Product Name</th>
<th>Weight</th>
<th>Price Per Gram</th>
<th>Quantity</th>
<th>Type</th>
<th>Category</th>
<th>Product Price</th>
<th>Image</th>
<th>Total Price</th>
<th class="text-center">Actions</th>
</thead>
<?php
$get = mysqli_query($conn,"SELECT * FROM stock;");
?>
<tbody>
<?php
while ($row=mysqli_fetch_array($get)) {
$id=$row['id'];
$company=$row['company_name'];
$name=$row['product_name'];
$weight=$row['weight'];
$price_per_gram=$row['price_per_gram'];
$quantity=$row['quantity'];
$type=$row['type'];
$category=$row['category'];
$price=$row['product_price'];
$img=$row['img'];
$total=$row['total_price'];
?>
<tr>
<td ><?php echo $id;?></td>
<td><?php echo $company;?></td>
<td><?php echo $name;?></td>
<td><?php echo $weight;?> g</td>
<td><?php echo $price_per_gram;?> $</td>
<td><?php echo $quantity;?></td>
<td><?php echo $type;?></td>
<td><?php echo $category;?></td>
<td><?php echo $price;?></td>
<td>
<img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
</td>
<td><?php echo $total;?></td>
<td style="width: 20px"> Edit
<button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
<?php
if (isset($_POST['delete'])) {
$delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." ");
header("location:viewstcok.php");
} ?>

Delete query not working to delete stock

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

While loop not working in PHP

While loop not working not showing anything.
My code is working when I use without creating a function on the same page but when I create function on functions.php and call it, it does not work.
<?php
include "include/db.php";
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
while ($row_search = mysqli_fetch_array($result_search)) {
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
}
}
?>
and
<?php
if(isset($_GET['search'])){
global $connection;
global $result_search;
search();
?>
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td><?php echo #$name_search; ?></td>
<td><?php echo #$f_search; ?></td>
<td><?php echo #$school_search; ?></td>
<td><?php echo #$roll_search; ?></td>
<td><?php echo #$email_search; ?></td>
<td><?php echo #$class_search; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You can see nothing happen:
Did you try to connect to the mysql before the execution of the query. Maybe, you have put the connect code in the other file, however couldn't see it here so just wondering.
your code should be like below, hope it will work to you.
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
return $row_search = mysqli_fetch_array($result_search);
}
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<?php
$search_results = search();
$i=1;
foreach($search_results as $row_search){
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td><?php echo $name_search; ?></td>
<td><?php echo $f_search; ?></td>
<td><?php echo $school_search; ?></td>
<td><?php echo $roll_search; ?></td>
<td><?php echo $email_search; ?></td>
<td><?php echo $class_search; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>

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>";
}
?>

Getting the option value without using submit button

Good day. Im trying to have some functionality on my project, but im stuck with this.
I want to display my database depending on what the user select
Here is my html code:
<table>
<tr><td>Select Request Status:</td>
<td><select class="form-control" name="status" style="width:200px;">
<option value="1">Pending Request</option>
<option value ="2">Requests On-Process</option>
<option value="3">Unassigned Requests</option>
<option>All Requests</option>
</select></td>
</tr>
</table>
And here is my php code:
if ($status = $_SESSION['status'] == 'Pending Request')
{
$query = "select * from tblrequest where status='Pending Request'";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table">
<tr>
<th>Request ID</th>
<th>Requestor</th>
<th>Department</th>
<th>Category</th>
<th>Assigned to</th>
<th>Status</th>
<th>Created Date</th>
<th>Created Time</th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody class="table table-hover">
<tr>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}
}?>
Im a newbie to php so please help me.
Can you try this,
HTML:
<form method="post">
<table>
<tr><td>Select Request Status:</td>
<td>
<?php $Statuses = array("0"=>"All Requests", "1"=>"Pending Request", "2"=>"Requests On-Process", "3"=>"Unassigned Requests" );?>
<select class="form-control" name="status" style="width:200px;" onchange="this.form.submit();">
<?php foreach($Statuses as $key=>$status):?>
<?php
$selected ="";
$statusPost = $_POST['status'];
if($key == $statusPost): $selected =" selected"; endif;
?>
<option value="<?php echo $key;?>" <?php echo $selected;?>><?php echo $status;?></option>
<?php endforeach;?>
</select></td>
</tr>
</table>
</form>
PHP:
if (isset($_POST['status'])){
$status= $_POST['status'];
$Where ='';
if($status!='0'){
$Where =" where status='".$Statuses[$status]."' ;
}
}
$query = "select * from tblrequest $Where";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table">
<tr>
<th>Request ID</th>
<th>Requestor</th>
<th>Department</th>
<th>Category</th>
<th>Assigned to</th>
<th>Status</th>
<th>Created Date</th>
<th>Created Time</th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody class="table table-hover">
<tr>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}
?>
put select dropdown in a form with id,action,method etc.
give id to a select element(say "mySelect")
Now, in jquery,
$("#mySelect").on("change",function(){
if($("#mySelect").val() != ""))
{
$("#form1").submit();
}
})

Categories