PHP,MySQL-将复选框与下拉列表关联
我有一个问题,试图添加一个“数量”的产品,一个人订购。我有一个Products表,一个orders表,一个order_item表(这是一个多对多表,其中包含来自Products和orders的id)。我在左边有一个下拉框,里面有你想要的数量。最大值是系统中可用的库存量 这是表单的外观:PHP,MySQL-将复选框与下拉列表关联,php,mysql,html,drop-down-menu,associative-array,Php,Mysql,Html,Drop Down Menu,Associative Array,我有一个问题,试图添加一个“数量”的产品,一个人订购。我有一个Products表,一个orders表,一个order_item表(这是一个多对多表,其中包含来自Products和orders的id)。我在左边有一个下拉框,里面有你想要的数量。最大值是系统中可用的库存量 这是表单的外观: <form name ="order_form" method="post" action="add_order_action.php" enctype="multipart/form-data">
<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>
现在,我要做的是从检查的产品名称中创建一个数组。我想做的是关联下拉菜单中的值,但前提是它们是选中的值之一。此外,如果选中值,则数量不能为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;
?>
我想我可能需要做一些JavaScript,但我在这方面绝对是垃圾
如果我不能解释自己:长话短说;我需要将数量添加到products数组中,但前提是产品已检查且数量>0
提前感谢,,
干杯:)
编辑:我做了一些更改,但现在出现以下错误:
错误1452:无法添加或更新子行:外键约束失败(
db/SEOrder\u items
,约束SEOrder\u items\u ibfk\u 2
外键(product\u id
)引用SEProducts
(id
)我认为您只需要稍微调整一下逻辑和模板
首先,您希望从订购页面上提供的产品而不是返回的产品推动整个过程;这:
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;
}
应该更像这样:
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;
}
$products = the_products_list_that_you_sent_to_the_page();
当然,如果产品列表非常庞大,那么您将无法像现在这样从页面中拉出产品列表(并验证每个产品)。但是,如果产品列表很小,那么从一个已知的有效列表开始就更容易了
问题的核心是您的数据库交互以及add\u order
的工作方式。您的添加订单
将在$products
中获取要查看的产品列表。您需要做的就是:
- 创建要插入的数据数组
- 检查
,如果产品已检查且有数量,则将其及其数量添加到列表中$products
- 完成上述循环后,检查订单列表中是否有任何内容:
- 如果订单列表中有某些内容,
可以将其全部添加到数据库中add\u order
- 如果订单列表中没有任何内容,
会返回错误代码(例如订单ID为零),并且调用者会向用户抱怨什么都不订购毫无意义add\u order
- 如果订单列表中有某些内容,
元素应该是:
<!--
Note that this also avoids the evil of having
duplicate id attributes in one page
-->
<select name="quantity_{$product.id}">
然后,当您查找特定产品的数量时,可以直接从$\u POST
中将其作为标量提取出来,如$q=$\u POST['quantity.'$product\u id]
我知道只要告诉您重命名
元素,我就可以解决您问题的根源,但我想我会在此过程中解决一些其他可能的问题。作为第一步,我将组合产品和数量数组,以便数量、复选框和产品名称位于一个数组中:
<select name="products[{$product.id}][quantity]" id ="quantities">
…
</select>
<input type="checkbox" name="products[{$product.id}][is_checked]" value="1" />
使用以下各项应能轻松通过此类阵列:
foreach($_POST['products'] as $currentProductId => $currentProductArray)
我做了这些更改,但现在当我尝试添加新订单时,我收到以下错误:错误1452:无法添加或更新子行:外键约束失败(
db/SEOrder\u items
,constraintSEOrder\u items\u ibfk\u 2
外键(product\u id
)引用SEProducts
(id
)
$_POST['products'] = array(
[1] => array(
'quantity' => 3,
'is_checked' => 1,
),
[2] => array(
'quantity' => 1,
// not checked
),
);
foreach($_POST['products'] as $currentProductId => $currentProductArray)