Select and Update form from table using php and mysqli - php

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

Related

Sorting HTML Table With TableSorter

I am attempting to add sortable columns to my html table and I thought I would give the jquery tablesorter a try. This is my syntax sans the actual DB call, and I think I have it set-up properly, however my table is not allowing me to sort. Why am I not able to sort?
<head>
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
<script>
$(document).ready(function()
{
$("#SaleDistro").tablesorter();
}
);
</script>
</head>
<table id="SaleDistro" class="tablesorter" border="1">
<thead>
<tr>
<th>Sales Name </th>
<th>Sales Region </th>
<th>Sales Count </th>
<th>Sales Supervisor </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tbody>";
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
print "</tbody>";
}
?>
</table>
</html>
EDIT --->
I edited my syntax to read like this, but still have the issue
</thead>
<tbody>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
}
?>
</tbody>
</table>
</html>
EDIT 2
Below is update to show how $query get's it's value
<head>
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
<script>
$(document).ready(function()
{
$("#SaleDistro").tablesorter();
}
);
</script>
</head>
<?php
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'username';
$option['password'] = 'password';
$option['database'] = 'database';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "Select SalesName, SalesRegion, SalesCount, SalesSupervisor from salesdata;";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table id="SaleDistro" class="tablesorter" border="1">
<thead>
<tr>
<th>Sales Name </th>
<th>Sales Region </th>
<th>Sales Count </th>
<th>Sales Supervisor </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tbody>";
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
print "</tbody>";
}
?>
</table>
</html>
You only want one <tbody> opening and closing tag each, so you need to move them out of the foreach loop.

showing each data from the database

I already show the list of names from the database, but the problem is I don't know how to show the information each user, once I click some user in my list her/his information will appear.
html
<div class="member_list">
<div class="list-unstyled">
<?php require_once "../function/admin_function.php"; ?>
</div>
</div>
<div class="information" id="table_information">
<table class="table_information">
<tr>
<th colspan="4">Information</th>
</tr>
<tr>
<th>lastname</th>
<th>address</th>
<th>contact</th>
</tr>
<tr>
<td>
<?php include "../function/information_function.php"; ?>
</td>
</tr>
</table>
</div>
information_function - php
<?php
include "../connection/connection.php";
$sql = "SELECT * FROM registration";
$result = mysqli_query($dbconn, $sql);
while ($row = mysqli_fetch_array ($result)){
echo "<tr>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "</tr>";
}
?>
user list - php
<?php
include "../connection/connection.php";
$sql = "SELECT * FROM registration";
$result = mysqli_query ($dbconn, $sql);
while($row = mysqli_fetch_array ($result)) {
echo "<ul class='table_php'>";
echo "<li>";
echo "<a href='#table_information' class='friends_link'>";
echo "<span class='chat-img pull-left'>";
echo "<img src='user.png' class='img-circle'>";
echo "</span>" . $row['lname'] . "</a>";
echo "</li>";
echo "</ul>";
}
?>
Put your code inside a function and just call that function after include statement.
//information_function//
<?php
include "../connection/connection.php";
function get_information(){
$sql = "SELECT * FROM registration";
$result = mysqli_query($dbconn, $sql);
while ($row = mysqli_fetch_array ($result)){
echo "<tr>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "</tr>";
}
}
?>
//html//
<div class="member_list">
<div class="list-unstyled">
<?php require_once "../function/admin_function.php"; ?>
</div>
</div>
<div class="information" id="table_information">
<table class="table_information">
<tr>
<th colspan="4">Information</th>
</tr>
<tr>
<th>lastname</th>
<th>address</th>
<th>contact</th>
</tr>
<?php include "../function/information_function.php"; ?>
<?php get_information(); ?>
</table>
</div>

Return only searched rows in table and hide other

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

Why is the script reading all values despite using "WHERE"?

I am creating a table that echo out replied messages and not replied messages.
My script for not replied messages worked well,only those not replied appeared.
But my other script for replied messages echo all data, both replied and not replied.
my script in one page,
<div class="container">
<div class="row">
<?php
require_once 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$sqlQueryStr = "SELECT * FROM feedback WHERE status = 0";
$result = mysqli_query($con, $sqlQueryStr);
while ($row = mysqli_fetch_array($result)) { // fetch the record
$feedback[$row['record']] = $row;
}
mysqli_close($con);
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<div class="container">
<h2>Customer Feedback - await for reply</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Relpy</th>
</tr>
</thead>
<?php
foreach ($feedback as $id => $info) {
echo '<tbody>';
echo '<tr>';
echo '<td>' . $info['name'] . '</td>';
echo '<td>' . $info['email'] . '</td>';
echo '<td>' . $info['message'] . '</td>';
echo '<td><button class="btn btn-info">' . 'Reply' . '</button></td>';
echo '</tr>';
echo '</tbody>';
}
?>
</table>
</div>
</div>
</div>
<div class="container">
<div class="row">
<?php
require_once 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$sqlQueryStr = "SELECT * FROM feedback WHERE status != 0";
$result = mysqli_query($con, $sqlQueryStr);
while ($row = mysqli_fetch_array($result)) { // fetch the record
$feedback[$row['record']] = $row;
}
mysqli_close($con);
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<div class="container">
<h2>Customer Feedback - Replied</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Relpy</th>
</tr>
</thead>
<?php
foreach ($feedback as $id => $info) {
echo '<tbody>';
echo '<tr>';
echo '<td>' . $info['name'] . '</td>';
echo '<td>' . $info['email'] . '</td>';
echo '<td>' . $info['message'] . '</td>';
echo '<td>' . 'Replied' . '</td>';
echo '</tr>';
echo '</tbody>';
}
?>
</table>
</div>
</div>
</div>
How can i change it?
Solve it by changing some of the codes in the second table
<?php
require_once 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$sqlQueryStr = "SELECT * FROM feedback WHERE status != 0";
$result2 = mysqli_query($con, $sqlQueryStr);
while ($row2 = mysqli_fetch_array($result2)) { // fetch the record
$feedback2[$row2['record']] = $row2;
}
mysqli_close($con);
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<div class="container">
<h2>Customer Feedback - Replied</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Relpy</th>
</tr>
</thead>
<?php
foreach ($feedback2 as $id2 => $info2) {
echo '<tbody>';
echo '<tr>';
echo '<td>' . $info2['name'] . '</td>';
echo '<td>' . $info2['email'] . '</td>';
echo '<td>' . $info2['message'] . '</td>';
echo '<td>' . 'Replied' . '</td>';
echo '</tr>';
echo '</tbody>';
}
?>
</table>
</div>

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

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

Categories