i build a project about online shop and there is a page in the admin dashboard that show the information about the orders that coming from the users ..there is many tables
there is orders table with Order.php class.
there is products table with Product.php class.
there is orderdetails table with OrderDetails.php class ..in this table there is
id,order_id,product_id.
every thing is working well put i can't show the $product['name'] and $product['price']
because $orderDet return null value , I don't know how to catch the id from orderdetails table to use it to get the product table data .
the getOne function from class OrderDetails.php
//get one
public function getOne($id){
$query="SELECT * FROM `orderdetails`
WHERE `id` = '".$id."'";
$result=$this->connect()->query($query);
$orderdetails=null;
if($result->num_rows == 1)
{
$orderdetails = $result->fetch_assoc();
}
return $orderdetails;
}
Orders.php where i show the the customer's information and the orders they buy for the admin
<?php
session_start();
require_once 'classes/product.php';
require_once 'classes/Order.php';
require_once 'classes/Orderdetails.php';
require_once 'classes/Category.php';
require_once 'inc/header.php';
$ord=new Order;
$ordDet=new OrderDetails;
$prod=new Product;
$orders=$ord->getAll();
if(!isset($_SESSION['id']))
{
header('location:Login.php');
die();
}
?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<table class="table">
<thead>
<tr>
<th>Customer Name</th>
<th>Customer Email</th>
<th>Customer Phone</th>
<th>Customer Address</th>
<th>Product Name</th>
<th>Product Price</th>
</tr>
</thead>
**the problem is here**
<?php
foreach($orders as $order)
{
$orderDetails=$ordDet->getOne($order['order_id']);
//var_dump($orderDetails);
$product=$prod->getOne($orderDetails['product_id']);
?>
<tbody>
<tr>
<td scope="row"><?php echo $order['customerName']; ?></td>
<td><?php echo $order['customerEmail']; ?></td>
<td><?php echo $order['customerPhone']; ?></td>
<td><?php echo $order['customerAddress']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo $product['price']; ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
</div>
</div>
<?php require_once 'inc/footer.php';?>
that is an image when i make var_dump($orderDetails)
Try the following.
$query = "SELECT * FROM `orderdetails` WHERE `id` != ''";
// or $query = "SELECT * FROM `orderdetails` WHERE `id` != 0";
$result = $con->query($query);
while($row = $result->fetch_assoc()){
$id = $row["id"];
echo "<h3>id is now $id</id>";
}
// The following line is where your problem lies
$query = "SELECT * FROM `orderDetails` WHERE `order_id` != ''";
/* I assume you know the best way to handle the following part */
$result = $con->query($query);
echo $result[0]["order_id"]; // or $result["order_id]
As an advice you need to have a way to select only new orders that you have not processed. You would not want your selection to include orders you have handled each time you want to get new data from the table.
I have to delete a record from database using a button but my delete query does not work. Records are entered in database successfully with insertion query. I followed exact tutorial for php code available on YouTube "How to delete records from database with PHP & MySQL" by "kanpurwebD". The code in tutorial works fine but my code still does not delete record. (I have 2 records entered in database).
My code is as follows:
<div class="container">
<div class="row">
<form action='add_record.php' method='get'><button type='submit' name='id' value='submit' class='btn btn-default'>ADD RECORD</button><br />
</form>
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Topic #</th>
<th>Name</th>
<th>Admin ID</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
echo '<br />';
$query = "SELECT * FROM tb_topic";
$result = $con->query($query);
if(isset($_POST['submitDeleteBtn'])){
$key = $_POST['keyToDelete'];
$check = "Select * from tb_topic where topic_id = '$key'";
if(mysqli_num_rows($con, $check)>0){
$query_delete = mysqli_query($con,"Delete from tb_topic where topic_id = '$key'");
echo 'record deleted';
}
}
while($query_row = mysqli_fetch_array($result)) {?>
<tr>
<td><?php echo $query_row['topic_id'];?></td>
<td><?php echo $query_row['topic_name'];?></td>
<td><?php echo $query_row['aid'];?></td>
<td><input type = 'checkbox' name = 'keyToDelete' value = "<?php echo $query_row['topic_id'];?>" required></td>
<td><input type="submit" name="submitDeleteBtn" class="btn btn-danger"></td>
</tr>
<?php }
?>
</html>
I got it resolved by using following statement:
if(isset($_GET['delete'])) {
$page = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$sql = "DELETE FROM tb_topic WHERE topic_id = $page";
}
$_GET() was not taking id as int so I tried typecasting it and it worked for me.
I have this report which filters data from 5 tables but it is very slow takes around 10 seconds. I tried using index on some columns but it did not help.
Basically the first query is the master and the others are for filtering it if the other queries condition met then it will skip it.
this is the script:
<div class="col-md-12">
<h3>List of Outstandings</h3>
<table class="table table-condensed table-bordered table-hover small">
<thead>
<tr>
<th >#</th>
<th>PR #</th>
<th>PR Type</th>
<th>Description</th>
<th>Dep.</th>
<th>Date</th>
<th>Requester</th>
<th>Assigned to</th>
</tr>
</thead>
<tbody>
<?php
$chkk = 0;
$sql = "SELECT * FROM msr WHERE Status ='Approved' ";
$result6 = $connn->query($sql);
if ($result6->num_rows > 0) {
$vo = 1;
while ($row0 = $result6->fetch_assoc()) {
$chkk = 0;
$MSRID = $row0["MSRID"];
$MSRType = $row0["MSRType"];
$result4 = "SELECT owner FROM tracking WHERE MSRID='$MSRID' ";
$MyRow4 = $connn->query($result4);
$row4 = $MyRow4->fetch_assoc();
$actionBy = $row4["owner"];
$resultusr = "SELECT RFQID FROM rfq WHERE MSRID='$MSRID' AND NOPO='No' ";
$MyRowusr = $connn->query($resultusr);
$rowusr = $MyRowusr->fetch_assoc();
$rfqcount = mysqli_num_rows($MyRowusr);
if ($rfqcount > 0) {
$chkk = 1;
}
$resultusr4 = "SELECT POID FROM po WHERE MSRID='$MSRID' ";
$MyRowusr4 = $connn->query($resultusr4);
$rowusr4 = $MyRowusr4->fetch_assoc();
$rfqcount4 = mysqli_num_rows($MyRowusr4);
if ($rfqcount4 > 0) {
$chkk = 1;
}
$resultusr1 = "SELECT MSRID FROM contract WHERE MSRID='$MSRID' ";
$MyRowusr1 = $connn->query($resultusr1);
$rowusr1 = $MyRowusr1->fetch_assoc();
$rfqcount1 = mysqli_num_rows($MyRowusr1);
if ($rfqcount1 > 0) {
$chkk = 1;
}
if ($chkk == 1) {
continue;
}
?>
<tr>
<td>
<?php echo $vo; ?>
</td>
<td>
<?php echo $row0["MSRID"]; ?>
</td>
<td>
<?php echo $row0["MSRType"]; ?>
</td>
<td>
<?php echo $row0["purposeofbuying"]; ?>
</td>
<td>
<?php echo depName($row0["DepRequester"]); ?>
</td>
<td>
<?php echo $row0["RequestDate"]; ?>
</td>
<td>
<?php echo reqName($row0["RequestPer"]); ?>
</td>
<td>
<?php echo reqName($actionBy); ?>
</td>
</tr>
<?php
$vo++;
}
}
?>
</tbody>
</table>
</div>
</div>
You can use subquery method instead of looping.
Example:
$sql = "SELECT *,
( SELECT owner
FROM tracking
WHERE tracking.MSRID= msr.MSRID
) AS _owner,
( SELECT RFQID
FROM rfq
WHERE rfq.MSRID= msr.MSRID
AND rfq.NOPO='No'
) AS _RFQID
FROM msr
WHERE rfq.Status ='Approved'";
Currently, When I type 'M' in the search engine I do not get any results inspite of having a 'Math' value in my database. (I am using Match Against function of mysql on my website). So I decided to make a code that first runs a Match Against function and if no output is obtained, it again runs the search but this time using 'LIKE' function of my sql and if still no result is obtained it shows alert 'no values found'.
the gist of code is somewhat like this..
mysql(Match against function)
If (mysqli_num_rows > 0)
{output}
elseif (mysqli_num_rows < 1)
{ mysqli (LIKE function)
then show output}
else {show alert'no results'}
Will this code give the result 'Math' when I enter 'M' in the search engine.
The actual code.
<?php
if (isset($_POST['go']))
{ $search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search))
{
if(isset($_POST['colleges']) )
{
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
}
else
{
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
$searchresults = mysqli_num_rows($query_run1);
?><p class = "totalresults"> <br />
<br />     <font size= "4" >Total Results :
<?php
echo $searchresults ;
?>
</font> </p>
<?php
if(mysqli_num_rows($query_run1)>0)
{
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
elseif(mysqli_num_rows($query_run1) < 1)
{
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?> <table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
}
?>
</table>
<?php
}
else
{
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
else
{ echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
}
You have some syntax issues in your code, your else statements were not lined up properly, so your page was not working. (For example, you had an } else { lined up with another } else {).
I went through all of the code and formatted it so I could see what the issue was, remember, Some sensible code indentation would be a good idea.
It helps us read the code and more importantly it will help you debug your code.
Take a quick look at a coding standard for your own benefit.
You may be asked to amend this code in a few weeks/months and you will thank me in the end.
<?php
if (isset($_POST['go'])) {
$search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search)) {
if(isset($_POST['colleges']) ) {
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
} else {
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
$searchresults = mysqli_num_rows($query_run1);
?>
<p class = "totalresults">
<br />
<br />
    <font size= "4" >Total Results: <?php echo $searchresults; ?></font>
</p>
<?php
if(mysqli_num_rows($query_run1)>0) {
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
<?php
} //end while loop
?>
</table>
<?php
} else if(mysqli_num_rows($query_run1) < 1) {
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip ;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
</table>
<?php
} //end while loop
} else { //num rows < 1
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
} else { //no searches returned
echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
I also suggest, for really complex, nested structures, to comment what each ending bracket goes to like I did in your code. I put comments so that I could see exactly what the } was ending.
WARNING: This code may be vulnerable to SQL Injection Attacks. You should Learn about Prepared Statements for MySQLi or PDO. I recommend PDO, which I wrote a function for to make it extremely easy, very clean, and way more secure than using non-parameterized queries.
If you could access the mysql.ini of your host, you could simply change the min word length for full text search.
[mysqld]
ft_min_word_len=N
Otherwise you should decide which search function you want to use. It's not very intuitive for users if one search is a full text search and the other is a simple LIKE.
One way around could also be to only use full text, if the user types more than 2 chars.
// ...
if (strlen($query) < 4) {
$where = "MATCH (...) AGAINST ...";
} else {
$where = "...LIKE ... ";
}
$sql .= $where;
Btw. MATCH AGAINST should normaly return way more results than like, so doing a LIKE search after MATCH AGAINST returns 0 rows, is kinda useless.
I need to retrieve the relevant data from database, and add to the attendance table again. this code shows many error. actually i dunno how to use the array to add the data. can anyone help?? thankssssssssssss.
<?php
$sql = "select p_module_ID, student_ID,
p_attendance_Date, p_attendance_Time,
p_attendance_Status,p_attendance_reason
from attendance";
$result = mysqli_query($con, $sql);
if(!$result)
{
echo mysqli_error($con);
exit();
}
while($rows = mysqli_fetch_array($result))
{
$attendance_list[] = array('p_module_ID' => $rows['p_module_ID'],
'student_ID' => $rows['student_ID'],
'p_attendance_Date' => $rows['p_attendance_Date'],
'p_attendance_Time' => $rows['p_attendance_Time'],
'p_attendance_Status' => $rows['p_attendance_Status'],
'p_attendance_reason' => $rows['p_attendance_reason']);
}
?>
<html>
<body>
<form action="attendance.php" method="post" accept-charset='UTF-8'>
<table border="0" cellspacing="20" >
<tr>
<td>
<select name="p_module_ID">
<?php
$sql = "SELECT p_module_ID FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_module_ID']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<select name="p_attendance_Date" >
<?php
$sql = "SELECT p_StartDate FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_StartDate']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<select name="p_attendance_Time">
<?php
$sql = "SELECT p_Time FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_Time']."</option>";
}
?>
</select>
</td>
</tr>
<table id="t01">
<tr>
<th> Student ID </th>
<th> Name </th>
<th> Attendance </th>
<th> Reason </th>
<?php foreach($attendance_list as $attend) : ?>
<tr>
<td>
<?php
$sql = "SELECT p.student_ID,CONCAT(s.student_fname, ' ', s.student_lname) AS fullname FROM Pals p JOIN student s ON p.student_ID = s.student_ID WHERE student_role = 'Student' GROUP BY student_ID";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo $attend[$row['student_id']];
}
?>
</td>
<td>
<?php
$sql = "SELECT p.student_ID,CONCAT(s.student_fname, ' ', s.student_lname) AS Fullname FROM Pals p JOIN student s ON p.student_ID = s.student_ID WHERE student_role = 'Student' GROUP BY student_ID";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo $attend['Fullname'];
}
?>
</td>
<td>
<?php echo $attend["p_attendance_Status"]; ?>
</td>
<td>
<?php echo $attend["p_attendance_reason"]; ?>
</td>
</tr>
</tr>
<?php endforeach; ?>
</table>
</table>
</form>
</body>
</html>
Use $row['student_Id'] instead of $row['student_id']. Same as for Fullname. You selecting AS fullname not AS Fullname. Listen to the case sensitiveness.