PHP,MySQL - 将复选框与下拉列表相关联

Jhe*_*eBz 8 html php mysql associative-array drop-down-menu

我在尝试向一个人订购的产品添加"数量"时遇到问题.我有一个产品表,订单表,一个order_item表(它是一个包含了ID的从产品和订单多到多表).我在左边有一个下拉框,里面有你想要的数量.最大值是系统中可用的库存.

这就是表单的样子:

在此输入图像描述

<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>
Run Code Online (Sandbox Code Playgroud)

产品名称和库存号均来自产品表:

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;
Run Code Online (Sandbox Code Playgroud)

现在,我所做的是我从被检查的产品名称中创建一个数组.我想要做的是关联下拉菜单中的值,但是如果它们是检查值之一.此外,如果检查一个值,数量不能为0.我可能这样做非常令人沮丧,但这是我认为最好的.我可以完成一个文本字段但是你必须清理用户输入.如果它可以正常工作,那就没关系=

我通过获取产品数组来添加订单:

<?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;
?>
Run Code Online (Sandbox Code Playgroud)

然后我的添加订单功能如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

我以为我可能要做一些JavaScript,但我绝对是垃圾.

如果我无法解释自己:长话短说; 我需要向products数组添加数量,但仅在检查产品且数量> 0时才需要.

在此先感谢,干杯:)

编辑:我做了一些更改,但现在我收到以下错误:错误1452:无法添加或更新子行:外键约束失败(db/SEOrder_items,CONSTRAINT SEOrder_items_ibfk_2FOREIGN KEY(product_id)REFERENCES SEProducts(id))

fee*_*ela 2

第一步,我将组合产品和数量数组,因此数量、复选框和产品名称位于一个数组中:

\n\n
<select name="products[{$product.id}][quantity]" id ="quantities">\n\xe2\x80\xa6\n</select>\n<input type="checkbox" name="products[{$product.id}][is_checked]" value="1" />\n
Run Code Online (Sandbox Code Playgroud)\n\n

将产生以下数组(其中 1 和 2 是产品 ID,仅在选中该框时才设置“is_checked”):

\n\n
$_POST[\'products\'] = array(\n    [1] => array(\n        \'quantity\' => 3,\n        \'is_checked\' => 1,\n    ),\n    [2] => array(\n        \'quantity\' => 1,\n        // not checked\n    ),\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n

使用以下方法应该很容易通过这样的数组:

\n\n
foreach($_POST[\'products\'] as $currentProductId => $currentProductArray)\n
Run Code Online (Sandbox Code Playgroud)\n