【问题标题】:PHP, MySQL - Associating a checkbox with a dropdownPHP,MySQL - 将复选框与下拉列表相关联
【发布时间】: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


    【解决方案1】:

    我认为你只需要稍微调整一下你的逻辑和你的模板。

    首先,您希望从订购页面上可用的产品而不是返回的产品中驱动整个事情;这个:

    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;
    }
    

    应该看起来更像这样:

    $products = the_products_list_that_you_sent_to_the_page();
    

    当然,如果产品列表很大,那么您将无法像现在这样将产品列表拉出页面(并验证每个产品)。但是,如果产品列表很小,那么从已知且有效的列表开始会更容易。

    问题的关键在于您的数据库交互以及add_order 的工作方式。您的add_order 会在$products 中获得要查看的产品列表。您需要做的就是:

    • 创建要插入的数据数组。
    • 通过$products,如果产品被检查并且有数量,则将其及其数量添加到列表中。
    • 在上述循环结束后,检查订单列表中是否有任何内容:
      • 如果订单列表中有内容,add_order 可以将其全部添加到数据库中。
      • 如果订单列表中没有任何东西,add_order 可以返回错误代码(例如订单 ID 为零),调用者可以向用户抱怨根本不订购没有任何意义。李>

    基本策略是收集您的数据然后对其采取行动,而不是在收集数据时尝试对您的数据采取行动。

    现在问题变成了:我们如何将数量与特定问题联系起来?这很容易解决,因为我们有产品 ID;你的 &lt;select&gt; 元素应该是:

    <!--
      Note that this also avoids the evil of having
      duplicate id attributes in one page
    -->
    <select name="quantity_{$product.id}">
    

    然后,当您查找特定产品的数量时,您可以将其直接从$_POST 中提取为$q = $_POST['quantity_' . $product_id] 的标量。

    我知道我可以通过告诉您重命名 &lt;select&gt; 元素来解决您的问题的根源,但我想我会在此过程中解决您可能遇到的一些其他问题。

    【讨论】:

      【解决方案2】:

      作为第一步,我会将产品和数量数组组合在一起,因此数量、复选框和产品名称都在一个数组中:

      <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)
      

      【讨论】:

      • 我进行了这些更改,但现在当我尝试添加新订单时,出现以下错误:错误 1452:无法添加或更新子行:外键约束失败(db/SEOrder_items,CONSTRAINT SEOrder_items_ibfk_2 外键 (product_id) 引用 SEProducts (id))
      猜你喜欢
      • 2014-06-16
      • 2014-09-13
      • 2015-01-29
      • 2015-12-14
      • 2019-02-26
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多