在foreach循环中提交多个数据

时间:2014-10-08 03:25:03

标签: php

我有一个代码,显示从复选框列表中检查的所有值,如下所示:

$query = "SELECT products_name, categories_desc, id, price
    FROM products, categories
    WHERE products.categories_id = categories.categories_id";

$result = mysql_query($query);
$last_category='';
echo "<table>";
while($row=mysql_fetch_array($result)){
if ($last_category!=$row['categories_desc']){
    echo "<tr><td><b>" . $row['categories_desc'] . "</b></td></tr>";
    $last_category=$row['categories_desc'];
}

echo "<td><input type='checkbox' id='shekbaks[]' name='shekbaks[]' value='" . $row['id'] . "'>" .
    $row['products_name'] . "</td>" .
    "<td>-</td><td>P" . $row['price'] . "</td></tr>";
}
echo "</table>";

然后,在用户选中并提交后,我使用文本框为其关联一个数字,以便他们可以指定所需数量,如下所示:

if( isset($_POST['shekbaks']) && is_array($_POST['shekbaks']) ) {
    echo "<form action='' method='post'><table>";

    foreach($_POST['shekbaks'] as $shekbaks) {

        //DISPLAY
        $query2 = "SELECT id, products_name, price FROM products WHERE id='" . $shekbaks . "'";
        $result2 = mysql_query($query2);
        while ($row2=mysql_fetch_array($result2)) {
            echo "&nbsp&nbsp<small><b>" . $row2['products_name'] .
            "<br> - - - - - - - - - - - - - - - - - - - - - - - - x</b>
            <input type='hidden' name='prods' value='" . $row2['id'] . "'>
            <input type='number' name='quant' style='width:30px;' min='1' value='1'></small><br>";
        }
    }
    echo "</table><input type='submit' name='submitOrder'></form>";
}

我想在表orders中使用字段

提交它们
  • products_id
但是我遇到了麻烦。我认为prods认为它可以帮助简化插入,因为两个数据都在输入类型中。可悲的是,我仍然不知道如何插入它们。

帮助任何人?提前谢谢!

2 个答案:

答案 0 :(得分:0)

您可能希望将input元素用作数组。使用产品ID作为数组键

<input type='number' name='quant[X]' style='width:30px;' min='1' value='1'>

其中X是产品ID,即$row2['id']

在表单提交后,您可以使用输入数组quant,其中数组元素的键是产品ID,值是数量。

foreach($_POST['quant'] as $product_id => $quantity) {
    // process here
}

答案 1 :(得分:0)

我会给你一个关于如何去做的起点。例如,在这里我假设一旦用户检查了他想要订购的产品并重定向到他应该选择数量的那个表单,表单就会被提交给某个foo.php脚本。负责将插入部分放入数据库中(只是为了保持代码清洁)。

您当前的代码可能类似于:

if( isset($_POST['shekbaks']) && is_array($_POST['shekbaks']) ) {
echo "<form action='foo.php' method='post'><table>";

//I'm defining an empty array to just store the checked product names for easy retrieval
$productnames=array();
foreach($_POST['shekbaks'] as $shekbaks) {

    //DISPLAY
    $query2 = "SELECT id, products_name, price FROM products WHERE id='" . $shekbaks . "'";
    $result2 = mysql_query($query2);
    while ($row2=mysql_fetch_array($result2)) {

        //Push the product name into the defined arrray
        array_push($productnames,$row2['products_name']);

        //Display info for the specific product
        //Store the quantity of the prodcut with the input name set as product name
        //This makes it unique and would help in retrieving it easily

        echo "&nbsp&nbsp<small><b>" . $row2['products_name'] .
        "<br> - - - - - - - - - - - - - - - - - - - - - - - - x</b>

        <input type='number' name='".$row2['products_name']."' style='width:30px;' min='1' value='1'></small><br>";
    }
}
$arr = serialize($productnames); //Serialize the array to pass it along with the form as an input
echo "</table><input type='hidden' name='productnames' value='$arr' />
<input type='submit' name='submitOrder'></form>";
}

现在提交此表单。数据使用foo.php请求发送到POSTfoo.php可能类似于:

$productnames = unserialize($_POST["productnames"]); //Get the array of product names
foreach ($productnames as $value) {
  //Here you can get the respective product name and associated quantity
  echo $value; //Product name
  echo $_POST[$value]; //Gives you the quantity as we chose the input names accordingly

 //Here you can insert the two values in orders table
}

当然有更有效的方法。这里消耗了额外的空间,因为我们传递了一个显式数组和表单。您也可以在输入名称中使用array keys,我建议您自己尝试一下。

我这样做是为了让您了解如何处理问题。这应该让你开始朝着正确的方向前进。