please why is the loop statement breaking, after the multiplication it only update the last row, tried using a foreach loop for the $totalprice in the update statement it says invalid argument.
if(array_key_exists('item', $_POST)){
// $items = $_POST['item'];
//foreach($_POST['item'] as $item){
//echo $item['Pquantity'] . ", ";
//echo $item['Pidno'] . ", ";
// }
//Loop through $_POST items, updating the database for each item
foreach ($_POST['item'] as $item) {
$Pquantity = intval($item['Pquantity']);
$Pidno = ($item['Pidno']);
//echo $Pquantity . ", ";
//echo $Pidno . ", ";
//$totalprice = intval($item['Pquantity'])
$queryreg = mysql_query("
UPDATE repplac
SET Pquantity = {$Pquantity}
WHERE
Pidno = '{$Pidno}'
AND
Uname = '{$_SESSION['username']}'
") or die(mysql_error());
}
}
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'") or die(mysql_error());
while ($row = mysql_fetch_assoc($pplresult))
{
$totalprice = $row['Price'] * $row['Pquantity'];
//echo "$totalprice";
//die();
$queryreg = mysql_query("
UPDATE repplac
SET Tprice = {$totalprice}
WHERE
Pidno = '{$Pidno}'
AND
Uname = '{$_SESSION['username']}'
") or die(mysql_error());
}
Neither arguments in the second condition gets changed inside the loop:
WHERE
Pidno = '{$Pidno}'
AND
Uname = '{$_SESSION['username']}
Perhaps you forgot to update $Pidno?
Also, I don't think the following is safe:
"SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'"
You might want to sanitize $_SESSION['username'] before passing it to the query.
Inside your foreach you update $Pidno with the following code
$Pidno = ($item['Pidno']);
Thus you are updating different records in the database. But inside your while loop $Pidno is staying the same the entire time. Thus you are updating the same record over and over. To fix this add the following code at the beginning of the while loop
while ($row = mysql_fetch_assoc($pplresult))
{
//get the correct Pidno for this row
$Pidno = ($row['Pidno']);
$totalprice = $row['Price'] * $row['Pquantity'];
// ... the rest of your while loop
Related
I tried to get multiple rows out from my database with PHP but all I get is one line of text like: "8910"
My code is following:
$sql = "SELECT * FROM posts WHERE idUsers=$id";
$sth = $conn->query($sql);
if(!$sth) {
echo("Error description: " . mysqli_error($conn));
die();
}
$rows = mysqli_num_rows($sth);
for ($x = 0; $x <= $rows; $x++) {
$sql = "SELECT idPosts FROM posts WHERE idUsers=$id";
if(!$sth) {
echo("Error description: " . mysqli_error($conn));
die();
}
$result = mysqli_fetch_array($sth);
$postId = $result['idPosts'];
echo $postId;
}
And then I edit this: echo $postId." ";
And get a space between the id's like this: 8 9 10.
I tried to do $postIds = explode(" ", $postId);
And then echoing out for example $postIds[0] but I get all the id's once again
Now I do not know what to do so I need help ^^
Replace $postId = $result['idPosts']; with $postId[] = $result['idPosts'];. That way you create an array an you can just access it like $postId[0].
You also forgot to query again.
...
$sql = "SELECT idPosts FROM posts WHERE idUsers=$id";
$sth = $conn->query($sql);
if(!$sth) {
...
Your second query is unnecessary, you already have all the data from your first query. Just replace your code with this:
$sql = "SELECT * FROM posts WHERE idUsers=$id";
$sth = $conn->query($sql);
if (!$sth) {
echo("Error description: " . mysqli_error($conn));
die();
}
$postIds = array();
while ($row = mysqli_fetch_array($sth)) {
$postIds[] = $result['idPosts'];
}
After this loop you will have an array of all the idPosts values. You can then process them as you need to. Or you can process them in the loop:
while ($row = mysqli_fetch_array($sth)) {
$postId = $result['idPosts'];
// code to process postId e.g.
echo "$postId<br/>";
}
$con=mysqli_connect("localhost","root","","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
error_reporting(E_ALL ^ E_NOTICE);
$query = "SELECT * FROM TT_posts WHERE post_status='publish' AND
ping_status='open'";
$result = $con->query($query);
while($row1 = $result->fetch_assoc())
foreach ($result as $row1){
$image = "SELECT * FROM TT_posts WHERE post_title='$row1[post_name]'";
$result1 = $con->query($image);
while($row2 = $result1->fetch_assoc())
foreach ($result1 as $row2){
echo "<img src='".$row2[guid]."'>";
echo "<p>".$row1[post_title]."</p>";
}}
?>
actually, below query returns 8 results.
$query = "SELECT * FROM TT_posts WHERE post_status='publish' AND
ping_status='open'";
When it executed the loop, it stops at the first result. I don't know what exactly stops the code.
I think you've got your variable names mixed up. Your first while assigns a row to $row1, then your foreach refers to $result, not $row1. The variable $row1 is actually being overwritten every time it loops around the foreach.
You don't actually need either foreach:
while ($row1 = $result->fetch_assoc()) {
$image = "SELECT * FROM TT_posts WHERE post_title='$row1[post_name]'";
$result1 = $con->query($image);
while ($row2 = $result1->fetch_assoc()) {
echo "<img src='".$row2[guid]."'>";
echo "<p>".$row1[post_title]."</p>";
}
}
This is the entire code the error must be in query 3 or 4. As you can see query 3 just gets info to build query 4 which should return the results.
I've got query 1 & 2 working ok which have the correct data showing.
<?php
session_start();
//printf('<pre>%s</pre>', print_r($_SESSION, true));
require('includes/config.inc.php');
require('includes/session.php');
//WORKING
DB_Connect();
$query = "SELECT * FROM food_delivery_orders_items WHERE food_delivery_orders_items.type = 'product' AND food_delivery_orders_items.order_id=".$_SESSION['pdf_quote']['id']."";
$result = mysql_query( $query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$hash = $row['hash'];
$foreignid = $row['foreign_id'];
$qty = $row['cnt'];
}
$query2 = "SELECT * FROM food_delivery_products WHERE food_delivery_products.id = ".$foreignid."";
$result2 = mysql_query( $query2) or die(mysql_error());
while($row = mysql_fetch_array($result2))
{
$name = $row['name'];
$description = $row['description'];
}
//---------------------------------------------------------------------------------------------------------------
$query3 = "SELECT * FROM food_delivery_orders_items WHERE food_delivery_orders_items.type = 'extra' AND food_delivery_orders_items.order_id=".$_SESSION['pdf_quote']['id']."";
$result3 = mysql_query( $query3)or die(mysql_error()) ;
while($row = mysql_fetch_array($result3))
{
$foreignidextra = $row['foreign_id'];
$qtyextra = $row['cnt'];
}
$query4 = "SELECT * FROM food_delivery_extras WHERE food_delivery_extras.id = ".$foreignidextra."";
$result4 = mysql_query( $query4) or die(mysql_error());
while($row = mysql_fetch_array($result4))
{
$nameextra = $row['name'];
}
echo $nameextra;
echo $qtyextra;
DB_Disconnect();
//$products = implode(", ",$_SESSION['pdf_quote']['product_arr']);;
//print $products
?>
print $row; is only valid inside the loop if you want to print all of them. Otherwise it will print only the last $row. Make it
while($row = mysql_fetch_array($result3))
{
$foreignidextra = $row['foreign_id'];
$qtyextra = $row['cnt'];
print $row;
}
its because you are printing the $row after the while loop gets executed
So it only prints the final result set
Use something like this instead:
$query3 = "SELECT * FROM food_delivery_orders_items WHERE food_delivery_orders_items.type = 'extra' AND food_delivery_orders_items.order_id=".$_SESSION['pdf_quote']['id']."";
$result3 = mysql_query( $query3) ;
while($row = mysql_fetch_array($result3))
{
$foreignidextra = $row['foreign_id'];
$qtyextra = $row['cnt'];
print_r($row);
}
you can also use the var_dump($row); instead of print_r($row);
this will output all the values stored in the $row array each time the loop iterates
I am having an issue with a while statement only returning one result. this is my first time trying to display product information from several tables and I don't know what I'm doing wrong. here is the code:
<?php
require('./includes/config.inc.php');
require(MYSQL);
$sql = sprintf("SELECT * FROM tea");
$res = mysqli_query($dbc, $sql);
if(!$res){
die('Could not complete query: '.mysqli_error($dbc));
} else {
echo 'Success!<br />';
while($row = mysqli_fetch_array($res)){
$table = $row['category'];
$inum = $row['item_number'];
echo $table.' '.$inum.'<br />';
$fetch = sprintf("SELECT sub_category, item_name, description FROM $table WHERE item_number ='$inum'");
$fetchRes = mysqli_query($dbc, $fetch);
if(!$fetchRes){
die('Could not fetch: '.mysqli_error($dbc));
} else {
while($fetchRow = mysqli_fetch_array($fetchRes)){
$subCat = $fetchRes['sub_category'];
$iNumber = $inum;
$iname = $fetchRes['item_name'];
$desc = $fetchRes['description'];
echo $id.' '.$subCat.' '.$iNumber.' '.$iname.' '.$desc.'<br />';
}
}
}
}
Inside your while loop, you should be using the $fetchRow variable as the array from which to grab columns, such as 'sub_category'.
IS
$subCat = $fetchRes['sub_category'];
$iNumber = $inum;
$iname = $fetchRes['item_name'];
$desc = $fetchRes['description'];
Needs to be: Res to Row
$subCat = $fetchRow['sub_category'];
$iNumber = $inum;
$iname = $fetchRow['item_name'];
$desc = $fetchRow['description'];
I'm currently adding fileID to idarray[] and then do a foreach. But in addition to fileID I also need serverID and id in the array and the foreach, how do I do this? So basically I need to find out how to add 2 more values to the array and then also use those extra values in the foreach.
$result = mysql_query("SELECT * FROM redundfiles WHERE removed=1");
while($row = mysql_fetch_array($result))
{
$idarray[] = $row['fileID'];
}
foreach ($idarray as $value)
{
$result = mysql_query("SELECT * FROM redundfiles WHERE fileID=$value AND removed=0 AND serverID=$XXXserverIDhereXXX");
while($row = mysql_fetch_array($result))
{
$sql = "DELETE FROM redundfiles WHERE id='".$XXXidhereXXX."' ";
mysql_query($sql) or die(mysql_error());
}
}
Why not just store the whole row into an array?
$rowarray[] = array();
$result = mysql_query("SELECT * FROM redundfiles WHERE removed=1");
while($row = mysql_fetch_array($result))
{
$rowarray[] = $row;
}
foreach ($rowarray as $row)
{
$fileid = $row['fileID'];
$serverid = $row['serverID'];
$result = mysql_query("SELECT * FROM redundfiles WHERE fileID=$fileid AND removed=0 AND serverID=$serverid");
while($row = mysql_fetch_array($result))
{
$sql = "DELETE FROM redundfiles WHERE id='".$XXXidhereXXX."' ";
mysql_query($sql) or die(mysql_error());
}
}
But really, your whole code piece here is a mess. You can accomplish this way more efficiently using JOINs.