How can I select a value from db - php

Im new in php and sql right now so im confused whats wrong in my codes.(Need to do)cart_tbl (order_id) food_name,special_request, quantity, amount are equal to order_id of my order_tbl. When both order_id of my order_tbl and cart_tbl is same. My output will be the value of that 2 table. This is my code right now.
<?php
$connect = mysqli_connect ("localhost", "root", "" , "db");
if(isset($_POST['order_id'])){
$asd = ($_POST['order_id']);
$sql = "SELECT food_name, special_request, quantity, amount
FROM cart_tbl
WHERE order_id='$asd'";
$result = mysqli_query($connect, $sql);
}
?>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Food</th>
<th>Special Request</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<?php
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["food_name"];?></td>
<td><?php echo $row["special_request"];?></td>
<td><?php echo $row["quantity"];?></td>
<td><?php echo $row["amount"];?></td>
</tr>
<?php
}
}
?>
</table>

Use INNER JOIN.
SELECT *
FROM cart_tbl
INNER JOIN order_tbl
ON cart_tbl.order_id = order_tbl.order_id
WHERE order_id='$asd'
Or, use NATURAL JOIN if the associated tables have the identical column name order_id and the columns are of same data type.
SELECT *
FROM cart_tbl
NATURAL JOIN order_tbl
WHERE order_id='$asd'

Related

How can i use the relationship between tables to show data in php and mysql

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.

HTML Table size too big after mysql_fetch_assoc

After I put the database values into the HTML table, the table gets to big. Any CSS code doesnt help. Shall I change something in the database value type? Or any other suggestion?
Below is the code:
<?php
$connector = mysql_connect('localhost','root','')
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("user_registration", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM login ORDER BY 1 DESC ");
?>
<table class="table1" border="2" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surename</th>
<th>Email</th>
<th>Gender</th>
<th>Username</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr >
<td >{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['surename']}</td>
<td>{$row['email']}</td>
<td>{$row['gender']}</td>
<td>{$row['username']}</td>
<td>{$row['password']}</td>
</tr>\n";
}
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
You need to select a subset of your records, otherwise the page will grow as fast as the database table... use the LIMIT statement to get back a page at a time...
SELECT id, name, etc
FROM MyTable
ORDER BY id
LIMIT (0, 20)
The next page is
SELECT id, name, etc
FROM MyTable
ORDER BY id
LIMIT (20, 20)
And so on.

How to fetch and show these data in php mysql?

I have question table and subject table and question table contain subject wise questions for online examination.
I need to fetch subject wise questions with subject name as header and show all the questions in subject wise serial number such as example: Maths: Q1, Q2, Q3
English: Q1, Q2, Q3 and so on.
How to achieve it in php and mysql. The question table and subject table are given below.
Question sample data are given below
<?php
require_once 'config.php';
//$con = mysqli_connect("localhost","root","","database_name");
$query1 = "SELECT q.q_id,q.setq_no, q.qtext_eng, s.sub_id, s.sub_name
FROM question q
INNER JOIN subject s ON s.sub_id = q.sub_id
INNER JOIN questionset qs ON qs.qset_id = q.qset_id
WHERE qs.qset_id =2 ORDER BY s.sub_id";
?>
<table class="table table-bordered">
<thead>
<tr>
<th>Q.No</th>
<th>Q Set number</th>
<th>Q text eng</th>
</tr>
<?php
$result1 = mysqli_query($link,$query1);
while($row1 = mysqli_fetch_array($result1))
{
$subID = $row1['sub_id'];
$subName = $row1['sub_name'];
?>
<h2><?php echo "$subName" ?></h2>
<?php
error_reporting(0);
$sno++;
$qSet = $row1['setq_no'];
$qEng = $row1['qtext_eng'];
?>
<tr>
<td><?php echo $sno; ?></td>
<td><?php echo $qSet; ?></td>
<td><?php echo $qEng; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
I am including some of your columns from question table here , you can add the rest same way
<?php
$con = mysqli_connect("localhost","root","","database_name");
$query1 = "SELECT q.q_id,q.setq_no, q.qtext_eng, s.sub_id, s.sub_name
FROM question q
INNER JOIN subject s ON s.sub_id = q.sub_id
INNER JOIN questionset qs ON qs.qset_id = q.qset_id
WHERE qs.qset_id =2 ORDER BY s.sub_id";
$presubID = 0;
<table class="table table-bordered">
while($row1 = mysqli_fetch_array($result1))
{
$subID = $row1['sub_id'];
if($subID != $presubID){
$subName = $row1['sub_name'];
<h2><?php echo "$subName" ?></h2>
$sno=0;
<thead>
<tr>
<th>Q.No</th>
<th>Q Set</th>
<th>Q text eng</th>
</tr>
</thead>
}
$presubID = $subID;
$sno++;
$qSet = $row1['setq_no'];
$qEng = $row1['qtext_eng'];
<tr>
<td><?php echo $sno; ?></td>
<td><?php echo $qSet; ?></td>
<td><?php echo $qEng; ?></td>
</tr>
<?php
}
?>
</table>

how to fetch colums data from two table using mysql with php using where

I have two Table ownership_profile and socity_unit.
Query For table1: select * from ownership_profile where SID='$id'
Query For Table2: select * from socity_unit where socity_id='$sid'
I have to join with one query, but i don't have idea how to do it.
This is my Php code but gives error:
<!-----------------Table For User Names-------------------------------------->
<table border="1" align="center">
<tr>
<th>Unit No</th>
<th>Member Name</th>
<th>Wing</th>
<th>Unit</th>
</tr>
<?php
if(isset($_GET['submit']))
{
$sql = "select * from ownership_profile o inner join society_unit s on o.sid = s.society_id where o.sid = '$sid' ";
$result = mysql_query($sql);
$i=1;
while($row=mysql_fetch_array($result)){
?>
<?php
$name = $row['NAME'];
$unitid = $row['UNIT_ID'];
$sid = $row['SID'];
$wings = $row['wings'];
$unit_no = $row['unit_no'];
{
?>
<!--User Submit Result-->
<tr>
<td><?php echo $unitid; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $wings; ?></td>
<td><?php echo $unit_no; ?></td>
</tr>
<?php }?>
<?php
//echo "<br>";
$i++;
}
}
?>
inner join is what you want.
select *
from ownership_profile o
inner join society_unit s
on o.sid = s.society_id
where o.sid = '$sid'
This assumes that 'sid' and 'society_id' are the relationship identifiers.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
select * from ownership_profile o inner join socity_unit s on o.SID = s.socity_id where o.SID = '$SOCIETY_ID'
This query working fine but...... its generate dublicate entry from table

Showing data correctly in html table with php

Hello this table pulls data from a database.
<h2>Weekly appointment list</h2>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Week Day</th>
<th>Customers</th>
<th>Selected service</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<?php
$date = date('Y-m-d', strtotime("this Monday"));
$sql = "SELECT * FROM appointment WHERE weekday = '$date'";
$query = mysqli_query($db, $sql);
$numRows = mysqli_num_rows($query);
if ($numRows > 0) {
while ($row = mysqli_fetch_array($query)) { ?>
<td><?php echo $row['user_name'] ?></td>
<td><?php echo $row['service'] ?></td>
<td><?php echo $row['time'] ?></td>
<?php }
}
?>
</tr>
</tbody>
But the problem is when i have two users from echo $row['user_name'] //user x, user y the table rows break and show something like this: image link. See the the table row is broken. I want to show this way:expected table structure. All customers are shown on the particular day row in customers column. How to fix my code or the way of representation. Thanks in advance.
change your sql query to group concat username,service and time.
$sql ="SELECT group_concat(user_name) as user_name,group_concat(service) as service ,group_concat(time) as time FROM appointment WHERE weekday = '$date'";
This query will return one row with all the user and service information in one row.
you want to combine something like this
With something like this:
$res = mysql_query(/**/);
$rows = array();
while($row = mysql_fetch_assoc($res)){
array_push($rows, $row);
}
Let me know if you struggle.

Categories