I have a mysql table with orders. I am trying to loop through the orders table and select individual client orders according to their user id and display the orders on their client accounts. However, my code below just prints the first row and repeats it endlessly jaming my browser every time.
what is wrong with this and how to i solve it
<?php
$records = $conn->prepare('SELECT * FROM orders WHERE user_id= :id');
$records-> bindParam(':id', $_SESSION['user_id']);
$records->execute();
$results=$records->fetch(PDO::FETCH_ASSOC);
$i=0;
while($i<=$results):
$i++;
?>
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td>#SJ<?=$results['id']; ?> </td><td><?=$results['academic_level']; ?></td><td ><?=$results['details']; ?></td>
</tr>
</table>
<?php
endwhile;
?>
Remove all $i related code. Just move your fetch statement to while condition, like the following:
while( $results=$records->fetch(PDO::FETCH_ASSOC))
You need to include the html code in the while loop because you want to display all of the orders.
I'm using this method, try this out.
Start while in first php section
<?php
$username = $_SESSION['username'];
$sql = $db->prepare("SELECT * FROM tableName WHERE username = '$username' ");
$sql->execute();
while($results=$sql->fetch(PDO::FETCH_ASSOC)){
?>
After that the html section coming:
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td><?php echo $results['id']; ?> </td><td><?php echo $results['academic_level']; ?></td><td ><?php echo $results['details']; ?></td>
</tr>
</table>
and here the ending
<?php
}
?>
Related
how do i select a single user transaction log from the database, it should fetch only the logged in transaction log
i tried with this line of code but it is fetching all the user transaction instead of the current logged in user. what can i do please
<?php
$sql = "SELECT * FROM admin_dept_table";
$query_run = mysqli_query($conn, $sql);
?>
<table class="display table" id="example">
<thead>
<tr class="info">
<th>Amount(USD)</th>
<th>Status</th>
<th>Tnx ID</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php
if(mysqli_num_rows($query_run) > 0)
{
while($row = mysqli_fetch_assoc($query_run)){
?>
<tr>
<td> <?php echo $row['amount']; ?></td>
<td> <?php echo $row['status']; ?></td>
<td> <?php echo $row['transactionid']; ?></td>
<td> <?php echo $row['date']; ?></td>
</tr>
<?php
}
}else{
echo "no record found";
}
?>
</tbody>
</table>
Think you can add condition to the SQL query that filtersby the user's ID or username. Example >>> SELECT * FROM table WHERE user_id = '$user_id'
I made a comment page to display comments that hadn't been approved by any Admins yet. On this page there is a button where you click approve to goto a seperate PHP file that will approve the comment you selected. The comment isn't being approved in my DB and the $SearchQueryParameter isn't even being displayed in my toast. It does however give me a success toast.
This is the approval file.
<!-- check DB File-->
<?php require_once("includes/DB.php"); ?>
<!-- check functions/errors File-->
<?php require_once("includes/functions.php"); ?>
<!-- check sessions File-->
<?php require_once("includes/sessions.php"); ?>
<?php
if(isset($_GET["id"])&&!empty($_GET["id"])){
$SearchQueryParameter = $_Get["id"];
global $ConnectingDB;
$Admin = $_SESSION["Username"];
$sql = "UPDATE comments
SET approvedby='$Admin', cmntstatus='ON'
WHERE id='$SearchQueryParameter'";
$Execute = $ConnectingDB->query($sql);
if ($Execute){
$_SESSION["SuccessMessage"]="Comment ".$SearchQueryParameter." Approved Successfully";
Redirect_to("Comments.php");
}else {
$_SESSION["ErrorMessage"]="There was an error!";
Redirect_to("Comments.php");
}
}
?>
Here is the comment file, It seems to be working fine as a whole. There is more but I think it's irrelevant.
<table class="table table-bordered table-hover">
<thead class="table-dark">
<tr>
<th>#</th>
<th>Date&Time</th>
<th>Author</th>
<th>Comments</th>
<th>Approve</th>
<th>Delete</th>
<th>ID</th>
</tr>
</thead>
<?php
global $ConnectingDB;
$sql = "SELECT * FROM comments WHERE cmntstatus='OFF'
ORDER BY id desc";
$stmt = $ConnectingDB->query($sql);
$Sr = 0;
while ($DataRows = $stmt->fetch()){
$CmntId = $DataRows["id"];
$DateTime = $DataRows["datetime"];
$CmntName = $DataRows["name"];
$Cmnt = $DataRows["comment"];
$Sr++;
if(strlen($CmntName)>10) {
$CmntName = substr($CmntName,0,10)."...";
}
if(strlen($DateTime)>10){
$DateTime = substr($DateTime,0,10)."...";
}
?>
<tbody>
<tr>
<td><?php echo htmlentities($Sr);?></td>
<td><?php echo htmlentities($DateTime); ?></td>
<td><?php echo htmlentities($CmntName); ?></td>
<td><?php echo htmlentities($Cmnt); ?></td>
<td>
Approve
</td>
<td>
Delete
</td>
<td>
<?php echo htmlentities($CmntId); ?>
</td>
</tr>
</tbody>
<?php
}?>
I’m not sure but I seem to recall that the superglobals in PHP are case-sensitive. Have you tried using $_GET["id"] instead of $_Get["id"]? –
rickdenhaan
6 mins ago
^ That fixed it ^
Hello I am working a project today. I want to print in front end the data of a specific user every time they login. When they login will get their user id and will fetch data from that specific user only.
I have this database. I want vendor_id 3 when this vendor login will display only values of specific vendor if no values will return "No Commission Data for this user"
Right now heres my wordpress wpdb code
<table border="1">
<tr>
<th>Vendor ID</th>
<th>Total Due</th>
<th>Time</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_pv_commission" );
$vendor = get_current_user_id();
foreach ( $result as $userdata) {
?>
<tr>
<td><?php echo $userdata->vendor_id;?></td>
<td><?php echo $userdata->total_due;?></td>
<td><?php echo $userdata->time;?></td>
</tr>
<?php
}
?>
</table>
And it returns the data of this. But what I want is only as per user like right now Im login as vendor id 3. It show all. What is the proper wordpress php conditioning code I need to use
Thank you for your answer.
Your code is right but your condition is wrong for selecting the data from database
when you don't apply where clause, MYSQL will always return all the records. so for that you need to provide where clause in your query
<table border="1">
<tr>
<th>Vendor ID</th>
<th>Total Due</th>
<th>Time</th>
</tr>
<?php
global $wpdb;
$vendor = get_current_user_id();
$result = $wpdb->get_results ( "SELECT * FROM wp_pv_commission where vendor_id=".$vendor );
foreach ( $result as $userdata) {
?>
<tr>
<td><?php echo $userdata->vendor_id;?></td>
<td><?php echo $userdata->total_due;?></td>
<td><?php echo $userdata->time;?></td>
</tr>
<?php
}
?>
</table>
How to use where clause in MySQL
I'm looking to upload a cvs into a db and then find how many times an instance of data appears to create a pick list.
I upload a cvs in to the db leaving me with
SKU and QUANTITY
I use to get the data from the db but I cant seam to find a way to group that data so their is only 1 sku for each item and a number of ordered items.
<table border="1" width="100%" id="table1">
<?php
$query = mysql_query("select * from pickcount");
while($fetch = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $fetch['sku']; ?></td>
<td><?php echo $fetch['quan']; ?></td>
</tr>
<?php
}
?>
</table>
<table border="1" width="100%" id="table1">
<?php
$query = mysql_query("SELECT sku, SUM(quan) AS Sum_Of_Quan FROM pickcount GROUP BY sku");
while($fetch = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $fetch['sku']; ?></td>
<td><?php echo $fetch['Sum_Of_Quan']; ?></td>
</tr>
<?php
}
?>
</table>
Try to replace your table structure with above structure and check the result
Replace your sql query to:
select sku, sum(quan) as sum_quan
from pickcount
group by sku
PS - you'll have to change
php echo $fetch['quan'];
to
php echo $fetch['sum_quan'];
I am trying to send over 2 vars to a page - 1 being the logged in user and the other is the ID of a registered user which is having there profile updated by the logged in user.
Im not sure how to change the page i have.
<?php
include("includes/connect.php");
//logged in users ID from logged in page.
$id = $_GET['id'];
$sql="SELECT * FROM guild_apply";
$result=mysql_query($sql);
?>
<blockquote>
<table border='1' width='100%'>
<tr>
<th>ID </th>
<th>Real Name</th>
<th>Character Name</th>
<th>Class</th>
<th>Rank</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
//info from all users registered with default rank (which is going to be changed)
<tr>
<td><?php echo $row['id']; ?> </td>
<td><?php echo $row['realname']; ?> </td>
<td><?php echo $row['charname']; ?> </td>
<td><?php echo $row['class']; ?> </td>
<td><?php echo $row['rank']; ?> </td>
<td align="center">update</td>
</tr>
<?php
}
?>
</table>
</blockquote>
?>
------update.php-----
<?php
// Connect to server and select database.
include("connect.php");
$id =$_GET['id'];
$rank =$_POST['rank'];
$charname =$_GET ['charname'];
// update data in mysql database
$sql="UPDATE guild_apply SET rank='$rank' WHERE charname='$charname'" or die ("cant find learner");
$result=mysql_query($sql) or die ("this stuffedup");
if ($result){
// if successfully updated.
header ("Location:../change.php?id=$id");
}else{
echo "Update failed";
}
?>
Add an &id= parameter to the URL.
<td align="center">update</td>