1

I have n number of textbox to get the name of the products and their quantity. So i used PHP array to get the value and its working fine. I tried the logic like if the user give duplicate products, then duplicate products is removed but their quantity is added with the same products.

<form method="post" action ="insert.php">
<input type =text name ="products[]" value = "">
<input type =text name ="quantity[]" value = "">
<!-- ...........
n numbers of textbox to add the products and quantity
--------- -->

<input type ="submit" value="submit"> </form>

insert.php:

$products = $_POST['products'];
$quantity = $_POST['quantity'];
for($i=0;$i<count($products);$i++)
 {
$query = "INSERT INTO table_name (`products`,`quantity`) VALUES ('$products[$i]','$quantity[$i]')";
mysql_query($query);
}

the array come like

$products[0] = "Apple"  $quantity[0] = "20"
$products[1] = "Orange"  $quantity[1] = "10"
$products[2] = "Apple"  $quantity[2] = "30"

Here products name apple is duplicate it occurs two times. So i remove the duplicate name apple and their quantity value should be added like 50(20+30)

2
  • so, if the product name is same then you want to add the quantity, right ? Commented Sep 5, 2015 at 11:06
  • @criesto - yes, right Commented Sep 5, 2015 at 11:07

2 Answers 2

1

First I would normalize the tables and just insert product_id(primary key) ,quantity and do the following (using binded vars):

INSERT INTO stock (product_id,quantity) VALUES ($1,$2 ) ON DUPLICATE KEY UPDATE quantity = quantity + $2

If there already are Apples (id=2) in the table we update the quantity

If you don't want to normalize the tables and use what you have the same applies:

  1. Make product column primary key

  2. Use INSERT INTO stock (product,quantity) VALUES ($1,$2 ) ON DUPLICATE KEY UPDATE quantity = quantity + $2

OR(but I don't really like it):

$distinct = array();
foreach($products as $index=>$product) {
    if(!isset($distinct[$product])) {
        $distinct[$product] = $quantity[$index];
    } else {
        $distinct[$product] += $quantity[$index];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. But in this table product_id is not a primary key.
I just said what I would do, read from the bolded text down :)
0

Try something like this:

$products = $_POST['products'];
$quantity = $_POST['quantity'];
$productQuantity = array();

foreach($products as $key => $product) {
    if (!array_key_exists($product, $productQuantity) {
        $productQuantity[ $product ] = 0;
    }
    $productQuantity[ $product ] += $quantity[ $key ];
}

Then you will end up with an array

$productQuantity['Apple'] => 50
$productQuantity['Orange'] => 10

So, to add it to the database, you could do:

foreach($productQuantity as $name => $qty) {
    $query = "INSERT INTO table_name (`products`,`quantity`) VALUES ('$name','$qty')";
    mysql_query($query);
}

1 Comment

its fine. If i have a another textbox name like price[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.