php


Array isn't fetching all data because of if statement


I believe my code isn't fully working because I have while loops inside a if statement, and is only allowing the loop to run once. The code is allowing an admin to view an order and access all of the information about that order. However with the code like it is, it only views one product, even if the order contains 2 or more. Below is my php code block
if (isset($_GET['orderid'])){
$targetID = $_GET['orderid'];
//query to find the item
$products_ordered = "";
$sql = mysql_query("SELECT * FROM `transactions` WHERE `order_id` ='$targetID' LIMIT 1");
$orderCount = mysql_num_rows($sql);
while ($transactions = mysql_fetch_array($sql)) {
//creating variables from the information
$order_id = $transactions["order_id"];
$mem_id = $transactions["mem_id"];
$OrderDate = $transactions["OrderDate"];
$ship_phone = $transactions["ship_phone"];
$ship_address = $transactions["ship_address"];
$ship_city = $transactions["ship_city"];
$ship_county = $transactions["ship_county"];
$ship_postcode = $transactions["ship_postcode"];
$ship_country = $transactions["ship_country"];
$order_details = mysql_query("SELECT * FROM `transactionDetails` WHERE `order_id` = $order_id") or die(mysql_error());
$orderDetailsCount = mysql_num_rows($order_details);
while ($row = mysql_fetch_array($order_details)) {
//creating variables from the information
$order_details_id = $row["Order_details_ID"];
$order_product_id = $row["Product_ID"];
$order_product_price = $row["Price"];
$order_product_quantity = $row["Quantity"];
$member_details = mysql_query("SELECT * FROM `members` WHERE `mem_id` = $mem_id") or die(mysql_error());
$memberDetailsCount = mysql_num_rows($member_details);
while ($row = mysql_fetch_array($member_details)) {
//creating variables from the information
$order_mem_fname = $row["mem_first_name"];
$order_mem_lname = $row["mem_last_name"];
$product_details = mysql_query("SELECT * FROM `products` WHERE `id` = $order_product_id") or die(mysql_error());
while ($row1 = mysql_fetch_array($product_details)) {
//creating variables from the information
$product_name = $row1["product_name"];
$products_ordered = "<tr>
<td width=\"20%\">Product Name</td>
<td width=\"80%\"><label> $product_name
</label></td>
</tr>
<tr>
<td width=\"20%\">Quantity</td>
<td width=\"80%\"><label> $order_product_quantity
</label></td>
</tr>
<tr>
<td width=\"20%\">Price per Item</td>
<td width=\"80%\"><label> $order_product_price
</label></td>
</tr>";
}
}
}
}
if ($orderCount == 0) {
echo "Sorry, order doesn't exist";
exit();
}
}
And this is my table which contains the data
<table>
<tr>
<td width="20%">Order ID:</td>
<td width="80%"><label><?php echo $order_id; ?> </label></td>
</tr>
<tr>
<td width="20%">Order Date</td>
<td width="80%"><label><?php echo $OrderDate; ?> </label></td>
</tr>
<tr>
<td width="20%">First Name</td>
<td width="80%"><label><?php echo $order_mem_fname; ?> </label></td>
</tr>
<tr>
<td width="20%">Last Name</td>
<td width="80%"><label><?php echo $order_mem_lname; ?> </label></td>
</tr>
<tr>
<td width="20%">Contact Number</td>
<td width="80%"><label><?php echo $ship_phone; ?> </label></td>
</tr>
<tr>
<td width="20%">Address</td>
<td width="80%"><label><?php echo $ship_address; ?> </label></td>
</tr>
<tr>
<td width="20%">City</td>
<td width="80%"><label><?php echo $ship_city; ?> </label></td>
</tr>
<tr>
<td width="20%">County</td>
<td width="80%"><label><?php echo $ship_county; ?> </label></td>
</tr>
<tr>
<td width="20%">Post Code</td>
<td width="80%"><label><?php echo $ship_postcode; ?> </label></td>
</tr>
<tr>
<td width="20%">Country</td>
<td width="80%"><label><?php echo $ship_country; ?> </label></td>
</tr>
<?php echo $products_ordered;?>
</table>
You save the output in a variable and the next time it goes through the code it will override the original value.
Instead of
$products_ordered = "<tr>
You should do that
$products_ordered .= "<tr> // notice the dot to concatenate the original html with the new html
If your first query returns 2 results and you expect 2 products that means each while loop within the main loop must loop at least once.
Print each count values and make sure the first one shows 2 and the other ones show 1. If it doesn't always show 1 it means your database is missing data.
You can also avoid this issue by using SQL JOIN. That way, you can directly test the final output with a SQL client or phpmyadmin.
Also, your code is open to SQL injection because you are using mysql_* functions and the query parameters are not sanitized. Instead you should use MySQLi or PDO. As a temporary solution, you could typecast $_GET['orderid'] to an integer or check if $_GET['orderid'] is an integer.
This is a complicated way to get your data, which could be done with some JOINs and fewer (perhaps one) queries.
Your problem is that you use $row at two levels : with $order_details and with $member_details. As you have only one member per order, there is nothing more to retrieve for the second order line.

Related Links

How to use array values outside foreach loop?
The service is currently unavailable Google api
Wordpress Plugin Sessions only work when logged in
validate payfort responce in custom php
Symfony multisite architecture without using CMF
Codeigniter - form_validator redirect page when not run. and form_validator not running
PHP Redirect to different pages after form submit (depends on what you write)
Zend Framework function that isn't an action in controller
Use PHP and Bootstrap to make a column of content for ACF
sh: virtuemart_user_id: command not found
SimpleXml php issue [duplicate]
Giving pictures the same projectID while inserting their path's into a table
$request->files->all() is always empty
how to delete old db records if user phone number already exist and resend the OTP
'Includes' in Convertdefs.php in SuiteCRM
fwrite result PHP

Categories

HOME
elasticsearch
xpath
flask
android-espresso
apache-nifi
yaml
xcode8.1
scroll
slider
semantic-web
qooxdoo
stored-procedures
sharepoint-online
mutation-testing
hystrix
watch-os-3
csrf
pushdown-automaton
corona
google-cloud-logging
gmp
raml
stackexchange.redis
ratio
web-config-transform
clipboard
iwebbrowser2
dnsmasq
region
winsock
windowsiot
opnet
caesar-cipher
lxml
xmlunit
custom-keyboard
busboy
cups
nanogallery
macports
elastica
unification
python-sounddevice
best-buy-api
netapp
system-on-chip
minikube
virtual-memory
sendinput
nuclio
wysihtml5
head.js
maven-jaxb2-plugin
skip-lists
gnome-terminal
network-protocols
kendo-editor
pecl
business-rules
kendo-treeview
cocoa-bindings
streamwriter
wininet
always-on-top
spring-mongo
coovachilli
between
wolfram-language
polymerfire
trust
vim-plugin
miniconda
perceptron
dsa
asp.net-web-api-helppages
flow-js
scala-macros
htmltidy
phonegap-facebook-plugin
hp-idol-ondemand
visual-studio-6
relationships
nidaqmx
screwturn
csslint
senchatouch-2.4
gpu-programming
cloo
smips
vlab
progress-db
application-server
codahale-metrics
instance-variables
coordinate
telerik-ajax
algol68
solandra
pinax
tacit-programming
isapi-redirect
levels

Resources

Encrypt Message