【发布时间】:2011-09-03 22:52:02
【问题描述】:
我在尝试向某人订购的产品添加“数量”时遇到问题。我有一个 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,但我绝对是垃圾。
如果我无法解释自己:长话短说;我需要在 products 数组中添加数量,但前提是检查了产品并且数量 > 0。
提前致谢, 干杯:)
编辑:我做了一些更改,但现在出现以下错误:
错误 1452:无法添加或更新子行:外键约束失败(db/SEOrder_items,CONSTRAINT SEOrder_items_ibfk_2 FOREIGN KEY (product_id) REFERENCES SEProducts (id))
【问题讨论】:
标签: php mysql html drop-down-menu associative-array