我在尝试向人们订购的产品添加“数量”时遇到问题。我有一个 Products 表、一个 orders 表、一个 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>
产品名称和库存编号均来自产品表:
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;
现在,我要做的是根据选中的产品名称创建一个数组。我想做的是关联下拉菜单中的值,但前提是它们是选中的值之一。此外,如果检查了一个值,则数量不能为 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;
?>
然后我的添加订单功能是这样工作的:
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;
}
我在想我可能必须做一些 JavaScript,但我在这方面绝对是垃圾。
如果我无法解释自己:长话短说;我需要将数量添加到产品数组,但前提是产品经过检查且数量 > 0。
提前致谢, 干杯:)
编辑:我做了一些更改,但现在出现以下错误:
错误 1452:无法添加或更新子行:外键约束失败 (db/SEOrder_items
, CONSTRAINT SEOrder_items_ibfk_2
FOREIGN KEY (product_id
) REFERENCES SEProducts
(id
))
最佳答案
作为第一步,我将合并产品数组和数量数组,因此数量、复选框和产品名称位于一个数组中:
<select name="products[{$product.id}][quantity]" id ="quantities">
…
</select>
<input type="checkbox" name="products[{$product.id}][is_checked]" value="1" />
将产生以下数组(其中 1 和 2 是产品 ID,“is_checked”仅在复选框被选中时设置):
$_POST['products'] = array(
[1] => array(
'quantity' => 3,
'is_checked' => 1,
),
[2] => array(
'quantity' => 1,
// not checked
),
);
使用这样的数组应该很容易:
foreach($_POST['products'] as $currentProductId => $currentProductArray)
关于PHP、MySQL - 将复选框与下拉列表相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6161944/