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

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>

Related

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

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>

Dynamic table with php pdo and two foreach

So, I'm connected to a database and I'm pulling data from two different tables within same database. I'm running foreach to generate rows, but the table renders not the way I want. I want the 2nd foreach to create a 2nd column to the right of the first one and all those dynamic rows to go downward.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/db.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row text-center">
<h3>Schedule of Ms. Kim & Ms. Morly</h3>
</div>
<?php
include 'database.php';
$pdo = Database::connect();
//Start of table Kim - ID=10
$sql = 'SELECT * FROM kim ORDER BY id';
$q = $pdo->prepare($sql);
$q->execute(array($schd_tut_id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$sql2 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=10 ';
$q2 = $pdo->prepare($sql2);
$q2->execute(array($tut_profile_id));
$data2 = $q2->fetch(PDO::FETCH_ASSOC);
$imgkim = $data2['tut_img'];
//End of Table Kim
//Start of table Morly - ID=16
$sql3 = 'SELECT * FROM morly ORDER BY id';
$q3 = $pdo->prepare($sql3);
$q3->execute(array($schd_tut_id));
$data3 = $q3->fetch(PDO::FETCH_ASSOC);
$sql4 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=16 ';
$q4 = $pdo->prepare($sql4);
$q4->execute(array($tut_profile_id));
$data4 = $q4->fetch(PDO::FETCH_ASSOC);
$imgmorly = $data4['tut_img'];
//End of table Morly
?>
<table class="table table-striped table-bordered">
<thead>
<th style="width:20px;">Time</th>
<th><img class="img-responsive" src="<?php echo $imgkim ?>"</th>
<th><img class="img-responsive" src="<?php echo $imgmorly ?>"</th>
<th><img class="img-responsive" src="<?php echo $imgmorly ?>"</th>
</thead>
<tbody>
<?php
//tutor kim
foreach ($pdo->query($sql) as $row ) {
echo '<tr>';
//echo '<td hidden>'. $row['id'] . '</td>';
//echo '<td hidden>'. $row['schd_tut_id'] . '</td>';
echo '<td>'. $row['time'] . '</td>';
if($row['status']=='available')
{ echo '<td>'. 'available';}
else {echo '<td>'. $row['status'] . '</td>';}
}
//echo '</tr>';
//tutor morly
foreach ($pdo->query($sql3) as $row ) {
//echo '<tr>';
//echo '<td hidden>'. $row['id'] . '</td>';
//echo '<td hidden>'. $row['schd_tut_id'] . '</td>';
echo '<td>'. $row['time'] . '</td>';
if($row['status']=='available')
{ echo '<td>'. 'available';}
else {echo '<td>'. $row['status'] . '</td>';}
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div> <!-- /container -->
</body>
</html>
Something like this should work, might need a bit of fiddeling.
Basically just store the values in an array and then print the array.
<?php
$row1 = [];
$row2 = [];
//table 1
foreach ($pdo->query($sql) as $row ) {
$row1[] = $row['status'];
}
//table 2
foreach ($pdo->query($sql2) as $row ) {
$row2[] = $row['status'];
}
echo "<table>";
foreach ($row1 as $key => $row) {
echo "<tr><td>" . $row[$key] . "</td><td>" . $row2[$key] . "</td></tr>";
}
echo "</table>";
Database::disconnect();
?>

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>

The Next iteration

I'm having some display problems here.
I have a "backend.php" file where I ask for two inputs.
These inputs are processed by "Addproducts.php" file and this file redirects to backend.php.
Backend.php also shows the current records in the database.
Here's the code for backend.php
<html>
<head>
<title>Inventory - Backend</title>
</head>
<body>
<form action="addproducts.php" method="post">
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="pname"/></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="text" name="productq"/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="Add Product"/></td>
</tr>
</table>
</form>
<h2>Current Products</h2>
<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$query = 'SELECT * FROM PRODUCTS';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '<table>';
echo '<tr>';
echo '<th>Product ID </th>';
echo '<th>Producr Name </th>';
echo '<th>Product Stock </th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
echo '<br/>';
echo '</table>';
}
else
{
echo "No products in the database";
}
}
?>
</body>
</html>
It displays something like this :-
Product ID Producr Name Product Stock
1 NewProduct 1
2HTC One5
3Samsung10
4Sony10
You see?
Only the first product is aligned, the rest are not.
How do I make them all align ?
Thanks.
The reason is you are closing your table tag within the loop, move it outside the loop like follows:
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
else
{
echo "No products in the database";
}
}
echo '<br/>';
echo '</table>';
Update: A better fix (see Barmar's comment below):
if (empty(mysql_num_row($result))) {
echo "<tr><td colspan='3'>No products in the database</td></tr>";
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
}
echo '</table>';
Also start looking into using mysqli(http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), mysql_ is deprecated!
I would suggest removing the
<br/>
from within your table. I believe that putting markup like line breaks inside of table markup will break the layout.

Categories