I have a script that allows an admin to view orders that customers have made, where they can decide to view the order details or delete the order. A user can order 1 or more items, for which entries in the database are created with the same order_id, but with different product_id's. When I display my orders these duplicate entries show up, however I only want 1 entry for every order_id. Below is my function
function viewOrdersAdmin(){
//This block grabs the orders
$order_list = "";
//Selecting all the orders in the table from that member
$sql = mysql_query("SELECT * FROM `transactions`") or die(mysql_error());
while ($transactions = mysql_fetch_array($sql)) {
//creating variables from the information
$order_id = $transactions["order_id"];
$mem_id = $transactions["mem_id"];
$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_product_id = $row["Product_ID"];
$member_details = mysql_query("SELECT * FROM `members` WHERE `mem_id` = $mem_id") or die(mysql_error());
$memberDetailsCount = mysql_num_rows($member_details);
while ($row2 = mysql_fetch_array($member_details)) {
//creating variables from the information
$order_mem_fname = $row2["mem_first_name"];
$order_mem_lname = $row2["mem_last_name"];
$order_list .= "Order ID:$order_id - Customer Name: $order_mem_fname $order_mem_lname <a href='manage_order.php?orderid=$order_id'>View</a> • <a href='manage_orders.php?deleteid=$order_id'>Delete</a><br/>";
}
}
}
if (count($orderDetailsCount) == 0) {
$order_list = "You have no orders to display";
}
print_r($order_list);
}
SELECT *
FROM transactions
GROUP BY order_id
Related
I am creating a grocery store for school and for each order I create it is supposed to grab a unique orderID from a database. I am supposed to be able let the user view unique orders using the orderID so the fact that it is always the same is a problem. to The orderID is set to auto increment but every time I submit an order it uses the same orderID. Here is my code
$email = $_SESSION['email'];
$name = $_SESSION['firstname'];
$shipping = $_SESSION['shipping'];
$ordertotal = $_SESSION['ordertotal'];
$tax = $_SESSION['tax'];
$productid = $_SESSION['productid'];
$count = $_SESSION['itemquantity'];
$linetotal = $_SESSION['linetotal'];
//get customer id
$custID = mysqli_query($connection, "SELECT CustomerID FROM Customers WHERE
Email ='$email' AND FirstName ='$name' ");
$customerid = mysqli_fetch_assoc($custID);
foreach($customerid as $i)
//print_r($i);
//insert customer id
$insertcustID = mysqli_query($connection, "INSERT INTO Orders (CustomerID)
VALUES ('$i') ");
//get order id
$orderid = mysqli_query($connection, "SELECT OrderID FROM Orders
WHERE CustomerID = '$i'
AND OrderDate IS NULL");
$id = mysqli_fetch_assoc($orderid);
foreach($id as $r)
print_r($r);
//get date
date_default_timezone_set("UTC");
$mydate=getdate(date("U"));
foreach($mydate as $d)
//echo "$mydate[weekday], $mydate[month] $mydate[mday], $mydate[year]";
//insert the order
$setOrder = mysqli_query($connection, "UPDATE Orders SET
ShippingCost='$shipping', Tax='$tax', Total='$ordertotal', OrderDate=' $d ’
WHERE OrderID='$r'
AND CustomerID='$i' ");
$insertOrder = mysqli_query($connection, "INSERT INTO OrderDetails
(OrderID, ProductID, Quantity, LineTotal)
VALUES ('$r', '$productid', '$count', '$linetotal')");
$allorders = mysqli_query($connection, "SELECT * FROM OrderDetails");
foreach($allorders as $q)
print_r($q);
unset($_SESSION["cart"]); //resets cart after order is submitted
include("realfooter.html");
oh ... it seems $i is a local variable , you declare $i variable inside foreach
foreach($customerid as $i)
//print_r($i);
//insert customer id
$insertcustID = mysqli_query($connection, "INSERT INTO Orders (CustomerID)
VALUES ('$i') ");
//get order id
$orderid = mysqli_query($connection, "SELECT OrderID FROM Orders
WHERE CustomerID = '$i'
you try put a temporary variable outside foreach, It will store the value of the variable
$customer_id ="";
foreach($customerid as $i) {
//print_r($i);
$customer_id = $i;
//insert customer id
$insertcustID = mysqli_query($connection, "INSERT INTO Orders (CustomerID)
VALUES ('$i') ");
}
//get order id
$orderid = mysqli_query($connection, "SELECT OrderID FROM Orders
WHERE CustomerID = '$customer_id'
AND OrderDate IS NULL");
Or set LIMIT for $custID
//get customer id
$custID = mysqli_query($connection, "SELECT CustomerID FROM Customers WHERE
Email ='$email' AND FirstName ='$name' LIMIT =1 ");
$customerid = mysqli_fetch_assoc($custID);
//insert customer id
$insertcustID = mysqli_query($connection, "INSERT INTO Orders (CustomerID)
VALUES ('".$customerid['CustomerID']."') ");
//get order id
$orderid = mysqli_query($connection, "SELECT OrderID FROM Orders
WHERE CustomerID = '".$customerid['CustomerID']."'
AND OrderDate IS NULL");
$id = mysqli_fetch_assoc($orderid);
foreach($id as $r)
print_r($r);
public function getotherclothdetail($id)
{
$qry1 = "SELECT rentprice from cloth_table where id = '$id' ";
$result2 = $this->fetch($qry1);
$qry = "SELECT cloth_table.*, user_table.email from cloth_table, user_table WHERE cloth_table.owner_id = user_table.id And cloth_table.price = $result2[0]['rentprice'] ";
$result = $this->fetch($qry);
return $result;
}
i want to display others cloth details from cloth_table where price is equal to selected cloth id price. help me out.
Apologies for vague title.
I currently have an edit form in which a product can be selected and the details are displayed in a form where they can be edited. Unfortunately, when edited every product in the product table is edited rather than just the product selected. To select the product i'm using where productname = productnameinput etc etc (Code below)
Database: http://prnt.sc/f4tf5g (Before an edit)
Tried adding the following WHERE statements at the end of the update query:
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE product.ProductName = :prodname
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE ProductName = :prodname
PHP:
// edit product in database
$query="
SELECT * FROM category
";
$result = $DBH->prepare($query);
$result->execute();
$categories = $result->fetchAll();
//we need to select all products frist
$query3 = "
SELECT * FROM product
";
$result3 = $DBH->prepare($query3);
$result3->execute();
$allProducts = $result3->fetchAll();
//When the Product is selected this function is run
if (isset($_POST['choose'])) {
$query2 = "
SELECT product.*, category.* FROM product LEFT JOIN category ON category.CategoryID = product.CategoryID WHERE product.ProductName = :prodname
";
$result2 = $DBH->prepare($query2);
$result2->bindParam(':prodname', $_POST['product_name']);
$result2->execute();
$product = $result2->fetch();
}
//When the Update button is Pressed
if (isset($_POST['update'])) {
$query4 = "
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE product.ProductName = :prodname
";
$result4 = $DBH->prepare($query4);
$result4->bindParam(':newCatId', $_POST['newcategory']);
$result4->bindParam(':newProdName', $_POST['productName']);
$result4->bindParam(':newProdDesc', $_POST['productDescription']);
$result4->bindParam(':newStock', $_POST['stockCount']);
$result4->bindParam(':prodname', $_POST['product_name']);
$result4->execute();
}
You are updating entire table, there is not filter in your update
$query4 = "
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock
";
i am trying to simplify my code which has multiple checks.. following is my code -
$query2 = "SELECT * FROM sample.temp";
$rslt = $dbo->query($query2);
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
$id = $item['entry_id'];
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
$stmt = $dbo->prepare("SELECT Qty FROM sample.temp WHERE vendor_ID=$vendor_ID AND component='$component' LIMIT 1");
if ($stmt->execute(array($_GET['Qty'])))
{
while ($row = $stmt->fetch(PDO::FETCH_OBJ))
{
$Q = $row->Qty;
}
}
$query4 = "UPDATE sample.stock SET Qty=(Qty+$Q) WHERE stock_ID=$vendor_ID AND component='$component'";//updating single existing entries
if ($dbo->query($query4))
{echo "Production updated !";}
else {echo "Production not updated!";}
$stm = $dbo->prepare("SELECT Qty FROM sample.temp WHERE vendor_ID=$vendor_ID AND component!='$component' LIMIT 1");
if ($stm->execute(array($_GET['Qty'])))
{
while ($row = $stm->fetch(PDO::FETCH_OBJ))
{
$Qt = $row->Qty;
}
$query5 = "INSERT INTO sample.stock (stock_ID, component, Qty) VALUES ($vendor_ID, '$component', $Qt)";//inserting new entry
if ($dbo->query($query5))
{echo "Production updated !";}
else {echo "Production not updated!";}
$st = $dbo->prepare("SELECT Qty FROM sample.temp WHERE vendor_ID!=$vendor_ID AND component='$component' LIMIT 1");
if ($st->execute(array($_GET['Qty'])))
{
while ($row = $st->fetch(PDO::FETCH_OBJ))
{
$Qtt = $row->Qty;
}
$query6 = "INSERT INTO sample.stock (stock_ID, component, Qty) VALUES ($vendor_ID, '$component', $Qtt)";//inserting new entry
if ($dbo->query($query6))
{echo "Production updated !";}
else {echo "Production not updated!";}
}
}
is there any way i can possibly reduce this code or change the way conditions are checked?
If i could use if statements in all three select statements it would be great !
Firstly i select a temporary table from where i transfer data into stock table on the basis of 3 checks -
updating existing values in stock table if they have same stock_ID and component as it is in temporary table
inserting new entry in stock table if it is not having the component corresponding to that stock_ID
inserting new entry in stock table if it is having the component but does not corresponds to any stock_ID in stock table
Note- vendor_ID in temporary table is same as stock_ID in stock table.
I am creating an inventory system for Magic the Gathering Cards and need to update the prices with the main card info.
I have two tables, Cards and Prices
Cards has the following columns:
ID, Name, Ed, Price
Prices has the following columns:
Name, Ed, Price
I need Cards.Price to be replaced with the value in Prices.Price.
Below is the code for two different attempts to make this work (I know I am probably making this a lot harder than it needs to be....)
Attempt #1
$query = "SELECT * FROM Cards";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$name=$row['Name'];
$ed=$row['Ed'];
$queryb = "SELECT Price FROM Prices WHERE Name='".$name."' AND Ed='".$ed."'";
$resultb = mysql_query($queryb) or die(mysql_error());
$rowb = mysql_fetch_array($resultb) or die(mysql_error());
$newPrice = $rowb['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."'");
}
Attempt #2
$queryCards = "SELECT * FROM Cards";
$queryPrices = "SELECT * FROM Prices";
$dblink = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $dblink);
$resultCards = mysql_query($queryCards) or die(mysql_error());
$resultPrices = mysql_query($queryPrices) or die(mysql_error());
$rowCards = mysql_fetch_array($resultCards) or die(mysql_error());
$rowPrices = mysql_fetch_array($resultPrices) or die(mysql_error());
if ($rowCards['Name']==$rowPrices['Name'] && $rowCards['Ed']==$rowPrices['Ed'])
{
$newPrice = $rowPrices['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE
Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'");
}
while($rowPrices = mysql_fetch_array($resultPrices))
{
if ($rowCards['Name']==$rowPrices['Name'] &&
$rowCards['Ed']==$rowPrices['Ed'])
{
$newPrice = $rowPrices['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE
Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'");
}
}
$rowPrices = mysql_fetch_array($resultPrices) or die(mysql_error());
while($rowCards = mysql_fetch_array($resultCards))
{
while($rowPrices = mysql_fetch_array($resultPrices))
{
if ($rowCards['Name']==$rowPrices['Name'] &&
$rowCards['Ed']==$rowPrices['Ed'])
{
$newPrice = $rowPrices['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE
Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'");
}
}
}
UPDATE Cards
JOIN Prices ON Cards.Name = Prices.Name AND Cards.Ed = Prices.Ed
SET Cards.Price = Prices.Price