0

I am creating an admin panel, which can insert products into database. The items are t-shirts, which have sizes, colors and quantity. The same item can have several sizes, colors and for each row it should have its own quantity. Eg. Product with id 100 can have sizeid 1 colorid 1 and quantity 2. Now the same product will be stored with same id 100, but different size, lets say sizeid 2, with same color, colorid 1 and different quantity. I am using checkboxes for the sizes and colors and fetch them with foreach.

add_panel.php

<input type="checkbox" name="color[]" value="<?php echo $colorname;?>">
</td>
<td><input type="text" name="quantity[]"></td>

add_success.php

$color = $_POST['color'];
$quantity = $_POST['quantity'];

<?php
    foreach ($color as $_colorvalue){
    $color = mysql_query("SELECT * FROM color 
        WHERE colorname = '$_colorvalue'");
    $fetch = mysql_fetch_array($color);
    extract($fetch);

foreach ($quantity as $_qtyvalue){
    echo $_qtyvalue;

    $insert_desc = mysql_query("INSERT INTO productdesc 
    (pid, productid_foreign, sizeid, quantity, colorid)
    VALUES ('', '$productid', '$sizeid', '$_qtyvalue', '$colorid')");

}}

When I make the same with the quantity inside those brackets, the item is stored multiple times, it executes both foreach. Now I need a solution to make a form that will store the same product, but with several sizes, colors and quantity in the table. Please help and ask, if I am not clear enough.

  • I think you need to show us a more complete example of your code... – swiss_blade May 11 '15 at 12:47
  • yes that was a type, thank you. – Agon Halili May 11 '15 at 12:51
  • Could I understand that you have the following tables in your database: `products`, `colors`, `sizes` and `quantities`? According to your description, if you don't have those tables, so there is a limitation or a mistake in your database design. – SaidbakR May 11 '15 at 12:53
  • I have table: products, which stores the name, description, keyword and dateadded. Table: color. Table: size and another table productdesc, which stores the ID of products, which takes the max ID of that table, the sizeid and colorid. The quantity is not a table, but is taken directly from the form – Agon Halili May 11 '15 at 12:57
  • @AgonHalili So, how could you store the quantity of an item there?!! – SaidbakR May 11 '15 at 12:59
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 11 '15 at 13:05
  • Why are you setting a Text field to an array? `` does not make sense. Please provide more details or a larger code example. – Twisty May 11 '15 at 15:35

0 Answers0