Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, MySQL - Associating a checkbox with a dropdown

I'm having an issue with trying to add a "quantity" to a product that a person is ordering. I've got a Products table, an orders table, an order_item table (which is a many-to-many table that contains the id's from both products and orders). I've got a dropdown box on the left which has the quantity that you want. The maximum value is the stock available in the system.

This is what the form looks like:

enter image description here

<form name ="order_form" method="post" action="add_order_action.php" enctype="multipart/form-data">
                    <table border=1 cellpadding=2 cellspacing=2>
                        <caption>Order Form</caption>
                        <tr>
                            <th align="right"> Property </th>

                            <th align="left"> Value </th>
                        </tr>
                        <tr>
                            <td class="col1"> Name </td>
                            <td class="col2"> <input type="text" name="customer" id ="customer" size=30> </td>
                        </tr>
                        <tr>
                            <td class="col1"> Address </td>
                            <td class="col2"> <input type="text" name="address" id ="address" size=30> </td>
                        </tr>
                        <tr>
                            <td class="col1"> Products </td>
                            <td class="col2">

                                <!-- Interests is an array of all interests in the database-->
                                {foreach $products as $product}
                                <select name="products[{$product.id}][quantity]" id ="quantities">
                                        {section name=quantities start=0 loop=$product.stock + 1 step=1}
                                        <option value="{$smarty.section.quantities.index}">{$smarty.section.quantities.index}</option>
                                        {/section}
                                </select>
                                <input type="checkbox" name="products[{$product.id}][is_checked]" value="1">{$product.name}<br>
                                {/foreach}
                            </td>
                        </tr>
                        <tr>
                            <td colspan=2 align="center">
                                <input type="submit" name="submit" value="Submit">
                                <input type="reset"  name="reset"  value="Reset">
                            </td>
                        </tr>
                    </table>
                </form>

The product names and stock numbers are both from the products table:

create table if not exists SEProducts
(
    id int not null auto_increment primary key,
    price double not null,
    name varchar(30) not null,
    stock int not null,
    original_stock int not null,
    sold_stock int not null
)ENGINE=INNODB;

Now, what I do is that I make an array from the product names that are checked. What I wanna do is associate the values from the dropdown menus, BUT ONLY IF THEY ARE ONE OF THE CHECKED VALUES. Additionally, if a value is checked, the quantity can't be 0. I'm probably doing this an extremely frustrating way but this is what I thought was the best. I could've done a text field but then you'd have to sanitise user input. That'd be okay if it works as opposed to this which doesn't =/

I add the orders by getting the product array:

<?php
include "includes/defs.php";

$customer = $_POST['customer'];
$delivery_address = $_POST['address'];
if (isset($_POST['products'])) 
{
    $products = $_POST['products'];
} 
else 
{
    $error = "An order must consist of 1 or more items.";
    header("Location: list_inventory.php?error=$error");
    exit;
}

$id = add_order($customer, $delivery_address, $products);

header("Location: order.php?id=$id");
exit;
?>

Then my add order function works like this:

function add_order($customer, $delivery_address, $products)
{
    $connection = mysql_open();
    $customer = mysql_escape_string($customer);
    $delivery_address = mysql_escape_string($delivery_address);

    $query = "insert into SEOrders (name, address, status) " .
             "values ('$customer', '$delivery_address', 'New')";
    $result = mysql_query($query, $connection) or show_error();

    $id = mysql_insert_id();

foreach ($products as $product)
    {
        if ($product['is_checked'] != 0 && $product['quantity'] != 0)
        {
            $query2 = "insert into SEOrder_items (order_id, product_id, quantity) " .
                    "values ('$id', '$product.id', '$product.quantity')";
            $result2 = mysql_query($query2, $connection) or show_error();
        }
    }

mysql_close($connection) or show_error();
return $id;
}

I was thinking I might have to do some JavaScript but I'm absolutely rubbish at that.

If I wasn't able to explain myself: Long story short; I need to add quantities to the products array but only if the products are checked and the quantity is > 0.

Thanks in advance, Cheers :)

EDIT: I've made some changes but now I get the following error: Error 1452 : Cannot add or update a child row: a foreign key constraint fails (db/SEOrder_items, CONSTRAINT SEOrder_items_ibfk_2 FOREIGN KEY (product_id) REFERENCES SEProducts (id))

like image 687
JheeBz Avatar asked May 28 '11 13:05

JheeBz


1 Answers

As a first step I would combine the products- and quantities-arrays, so the quantities, the check-box and the product-name are in one array:

<select name="products[{$product.id}][quantity]" id ="quantities">
…
</select>
<input type="checkbox" name="products[{$product.id}][is_checked]" value="1" />

would result in following array (where 1 and 2 are the products-ids, 'is_checked' is only set if the box was checked):

$_POST['products'] = array(
    [1] => array(
        'quantity' => 3,
        'is_checked' => 1,
    ),
    [2] => array(
        'quantity' => 1,
        // not checked
    ),
);

It should be easy to get through such an array using:

foreach($_POST['products'] as $currentProductId => $currentProductArray)
like image 176
feeela Avatar answered Oct 18 '22 07:10

feeela