I am working on 3 tables linked with foreign keys and also set to ON UPDATE CASCADE and ON DELETE CASCADE. These tables are category, subcategory and products.
products table - PID(PK), productname, subcatid(FK)
subcategory table - subcatid(PK), subcategoryname, catid(FK)
category table - catid(PK), categoryname
What is the right query to UPDATE products table when I want to change the name of the product? Also if I want to change the subcategory of a product by using a form?
I am using a form which successfully prepopulates with the fields - product name, subcategory name but it does not update the records in products table, means it does nothing and does not changes the product name and subcategory name.
Any help is appreciated.
am using the following code
<?php
// Parse the form data and add inventory item to the system
if (isset($_POST['PRODUCT_NAME'])) {
$pid = mysql_real_escape_string($_POST['thisPID']);
$catalog_no = mysql_real_escape_string($_POST['CATALOG_NO']);
$product_name = mysql_real_escape_string($_POST['PRODUCT_NAME']);
$price = mysql_real_escape_string($_POST['PRICE']);
$composition = mysql_real_escape_string($_POST['COMPOSITION']);
$size = mysql_real_escape_string($_POST['SIZE']);
$subcat = mysql_real_escape_string($_POST['SUBCAT_ID']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("UPDATE products SET CATALOG_N0='$catalog_no', PRODUCT_NAME='$product_name', PRICE='$price', COMPOSITION='$composition', SIZE='$size', SUBCAT_ID='$subcat' WHERE PID='$pid'");
header("location: inventory_list.php");
exit();
}
?>
<?php
// Gather this product's full information for inserting automatically into the edit form below on page
if (isset($_GET['pid'])) {
$targetID = $_GET['pid'];
$sql = mysql_query("SELECT products.PID, products.CATALOG_NO, products.PRODUCT_NAME, products.PRICE, products.COMPOSITION, products.SIZE, products.SUBCAT_ID, subcategory.SUBCAT_ID, subcategory.SUBCATEGORY_NAME FROM (products, subcategory) WHERE subcategory.SUBCAT_ID=products.SUBCAT_ID AND PID='$targetID' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$catalog_no = $row["CATALOG_NO"];
$product_name = $row["PRODUCT_NAME"];
$price = $row["PRICE"];
$composition = $row["COMPOSITION"];
$size = $row["SIZE"];
$subcat = $row["SUBCAT_ID"];
}
} else {
echo "You dont have that product";
exit();
}
}
?>
You're not using the product name as a foreign key, so a simple standard
UPDATE products SET productname='New Name of Product' where PID=XXX;
would do the trick. Same goes for the subcatid in products. As long as the new subcat ID exists in the subcategory table, you can change products.subcatid to whatever you want, again via a simple
UPDATE products SET subcatid=New_ID where PID=XXX;
Related
I have been trying to create a persistent MySQL-based shopping cart application starting from a session-based php shopping cart. The cart table has the structure userid | productid | quantity | postingdate.
In the session-based implementation, the quantity info for a certain product was stored in $_SESSION['cart'][$row['id']]['quantity'];; I am facing trouble with retrieving the quantity info for a certain product from the cart table in the database because I can't seem to find how to extract the productId necessary for the $getquantity query.
$sql1 = "SELECT * FROM cart WHERE userid='$userId'"; //selects the user's cart
$query1=mysqli_query($con,$sql1);
if (!empty($query1)) { //if the user's cart isn't empty
while($row = mysqli_fetch_array($query1)){
$query = mysqli_query($con,"SELECT * FROM products JOIN cart ON cart.productid=products.id
ORDER BY cart.postingDate DESC"); //selects all the products in the current user's cart
}
$totalprice=0;
$totalqunty=0;
if (!empty($query)) {
while ($row = mysqli_fetch_array($query)) { //for each product
//$quantity=$_SESSION['cart'][$row['id']]['quantity']; //SESSION IMPLEMENTATION
$id = $row['id'];
$quantity = mysqli_query($con,"SELECT quantity FROM cart WHERE userid='$userId' AND productid='$id'"); //MY IMPLEMENTATION, THE WRONG PART IS WITH $id
$subtotal= $quantity*$row['productPrice']+$row['shippingCharge'];
$totalprice += $subtotal;
$_SESSION['qnty']=$totalqunty+=$quantity; //SESSION IMPLEMENTATION
}
}
}
I've tried $row['id'] but it returns an array of all the productid from the records returned by $query and MySQl doesn't seem to accept it, while instead I need the $getquantity to run on each productid. Please don't mind how it's vulnerable to SQL injections as I will get to that in later stages.
Here's a working solution: I could have just selected the cart.quantity attribute (considering how the products and cart table were joined):
$sql1 = "SELECT * FROM cart WHERE userid='$userId'"; //selects the user's cart
$query1=mysqli_query($con,$sql1);
if (!empty($query1)) { //if the user's cart isn't empty
while($row = mysqli_fetch_array($query1)){
$query = mysqli_query($con,"SELECT products.productImage1 , products.productName ,
products.id, cart.productId, cart.quantity as qty, products.productPrice,
products.shippingCharge FROM products JOIN cart ON cart.productid=products.id
ORDER BY cart.postingDate DESC"); //selects all the products in the current user's cart
}
$totalprice=0;
$totalqunty=0;
if (!empty($query)) {
while ($row = mysqli_fetch_array($query)) { //for each product
$quantity=$row['qty'];
$subtotal= $quantity*$row['productPrice']+$row['shippingCharge'];
$totalprice += $subtotal;
}
}
}
I'm developing a shopping cart using php, now I stuck at some point as follow.
There are tables like: customers, products, orders, order_detail
products table:
id | name | quantity
order_detail table:
order_id | product_id | quantity
I completely done for add product to cart and customer can submit the order to database, admin can remove or delete orders from backend and customers can remove orders list or delete orders from frontend if that ordered still not confirm payment.
Example:
"customer1" order 10 products, order ID is #907987899 and "product1" customer1 order quantity = 2, "product2" customer1 order quantity = 5 and so on.....
if the ordered still not confirm then customer still can remove product from the ordered list, and also can delete the entire ordered.
My problem now is: if Customers or Admin cancel or delete the ordered that still not confirm payment then I want to return product quantity from order_detail table list back to products table. by the time when customer submitted the order then product quantity is subtract from product table.
Here is my code that return quantity back to products:
require '../../init.php';
require $ROOT . '/functions/basic.php';
if(isset($_POST['id']))
{
$id = htmlspecialchars($_POST['id']);
$delete = dbQuery("DELETE FROM orders WHERE order_id = '$id'");
if($delete == true)
{
//unlink($ROOT . '/upload/products/' . $image);
$select_product = dbAll("SELECT product_id, quantity FROM order_detail WHERE order_id = '$id'");
foreach($select_product as $product)
{
$quantity = $product['quantity'];
$product_id = $product['product_id'];
$return_product_quantity = dbQuery("UPDATE products SET quantity = (quantity + $quantity) WHERE product_id = $product_id");
}
if($return_product_quantity == true)
{
$delete_order_detail = dbQuery("DELETE FROM order_detail WHERE order_id = '$id'");
if($delete_order_detail == true)
{
echo "OK";
}
else
{
echo "Can't delete order detail information";
}
}
}
else
{
echo "Can't delete order row";
}
}
but the query it seem not work as my required, so please help me how to handle this kind of logic to perform my required.
Thanks in advance.
You are trying to use the variable quantity in your UPDATE even though it has no value. Try this instead:
$return_product_quantity = dbQuery("UPDATE products
SET quantity = ((SELECT quantity FROM products WHERE product_id = $product_id) + $quantity)
WHERE product_id = $product_id");
Thanks in advance for any time you spend on my question.
I am trying to display data in a way that will display the manufacturer as a name instead of a number.
Basically when they store the data they choose a manufacturer from a drop down which is generated from a table.. IE Trogues = 1 so products stores the #1 so I know that any beer is associated with trogues is 1. Now I want to display the data but instead of having a 1 I would like to have Trogues be displayed. Where you see manufacturer in the echo code below..
I am not understanding the process logic here..
error_reporting(E_ALL);
ini_set('display_errors', 1);
$sql = "SELECT * FROM products
LEFT JOIN manufacturer
ON product.manufacturer = manufacturer.id
ORDER BY manufacturer.id, product.id";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "
<div class=reportclientproduct>".$row['manufacturer']." - <a href=".$row['website']." target=_blank>".$row['product']."</a></div>";
}
Have you tried the query like this:
$sql = "SELECT man.id AS manufac, products.product AS prod FROM products
LEFT JOIN manufacturer as man
ON product.manufacturer = manufacturer.id
ORDER BY manufacturer.id, product.id";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "
".$row['manufac']." - ".$row['prod']."
";
}
Assuming that the table products had a column named manufacturer which holds the ID of the manufacturer, and that both tables have columns name ID which hold the ID of the table item.
Also the JOIN functions may vary based on the database you use. But the aforementioned method is for mysql.
I am working on a php project with MySQL as database. Here is the table structure
product_id (PK)
shop_id (FK)
product_name
product_desc
Product_id is the primary key and shop_id is the foreign key (shows the product belongs to which shop). Now I want to select these product and display them in a <div>. For each shop products, I want to display a different <div>. e.g if there are three products say p1,p2 and p3, and they have same shop_id (say) 1, i want them to show in a separate <div>. there will be separate <div> for each shop_id, and all products belong to that shop_id will be displayed in that <div>. How can I do this with My_SQL query or using PHP. I have used a simple SELECT query like this but with this i could not find a way to initiate different <div> for separate shop.
$q = mysql_query("SELECT * FROM table");
echo "<div>";
while($product = mysql_fetch_assoc($q)){
echo $product['product_name']."<br/>";
}
echo "</div>";
It just throw all products in a single dive, I want separate div for separate shop (which contains all products of that shop). Thanks for reading..
Simplest way is to group the products by id in a array and loop through them
$all_products = array();
while($product = mysql_fetch_assoc($q)){
$all_products[$product['shop_id']][] = $product;
}
foreach($all_products as $shop_id => $product_array){
echo "<div id='shop_".$shop_id."'>";
foreach($product_array as $product){
echo $product['product_name']."<br/>";
}
echo "</div>";
}
OR call product data fro each shop id
$q = mysql_query("SELECT * FROM shops");
while($shops = mysql_fetch_assoc($q)){
$product_q = mysql_query("SELECT * FROM product_table WHERE shop_id=$shops['id']");
echo "<div>";
while($product = mysql_fetch_assoc($product_q)){
echo $product['product_name']."<br/>";
}
echo "</div>";
}
I have two table in mysql that is product and sell. The product contain the stock and the sell contain the purchasing record. Now i want to sell a product from the stock when i insert the quantity of selling item into the sell table then the same amount should be decrease in the stock that is product table. Please help me for the query.
Sell table insertion.
$cus_id = $_POST['customer'];
$reg_id = $_POST['cus_region'];
$cat_id = $_POST['product'];
$name = $_POST['name'];
$price = $_POST['unit_price'];
$quantity = $_POST['quantity'];
$date = $_POST['datepicker'];
$total = $price * $quantity;
$payment = $_POST['payment'];
$remainig = $total - $payment;
$query = "INSERT INTO sell SET cus_id = '".$cus_id."',reg_id = '".$reg_id."', cat_id = '".$cat_id."',product = '".$name."',selling_price = '".$price."',selling_date = '".$date."',item_quantity = '".$quantity."',amount_paid= '".$payment."',total_price = '".$total."', remaining = '".$remainig."'";
$res = mysql_query($query);
Two way to resolve that,
1) if you are getting the quantity of product you can make second query to deduct the quantity from stack table (product table)
2) You can use trigger for such operations, while you insert in to sales table that same quantity you can reduce from the product table
You have to just change or decrease the quantity of the product in PRODUCT table. So, you have to use UPDATE query.
This can solve your issue:
1) I consider that your cat_id is the unique product key to identify a product in your PRODUCT table.
2) I consider that you have quantity column in PRODUCT table, which store the quatity of the product. And 1 product is being sold at a time.
UPDATE TABLE_NAME SET quantity=quantity - 1
WHERE cat_id = "SOME_INT_ID_WITHOUT_QUOTES"