简体   繁体   English

根据所选复选框将所有值插入一行

[英]Insert all the values in a row based on a selected checkbox

I have the following table that fetches data from the database: 我有下表从数据库中获取数据:

while($row = mysql_fetch_array($result)) 
{
echo '
    <tr>
        <td width="320">
            <input type="checkbox" name="product_ID[]" value="'.$row['Product_ID'].'" />'.$row['Product_description'].'<br />
        </td>
        <td width="50">
            <input type="text" name="product_cost[]" value="'.$row['Product_Retail_cost'].'" maxlength="3" size="3" />
        </td>
        <td width="50">
            <input type="text" name="product_quantity[]" value="1" maxlength="3" size="1" />
        </td>
    </tr>';
}

What I would like to do, is insert the row into a table, making sure to insert any updated values from the two text boxes. 我想做的是将行插入表中,确保从两个文本框中插入任何更新的值。 The insert looks like this: 插入看起来像这样:

$product_ID = $_POST['product_ID'];
$product_cost = $_POST['product_cost'];
$product_quantity = $_POST['product_quantity'];

for ($i=0; $i<sizeof($product_ID);$i++)
    {
    $query="INSERT INTO mjj_ordered_products 
                (`Order_ID`, 
                `Product_ID`, 
                `Product_Quantity`, 
                `Product_Price`)
    VALUES ((SELECT MAX(Order_ID) AS Order_ID FROM `mjj_orders`),
                '".$product_ID[$i]."',
                '".$product_quantity[$i]."',
                '".$product_cost[$i]."')";
    mysql_query($query) or die (mysql_error());
    }

But while this inserts the correct Product_ID, the remaining two values inserted do not correspond to the selected checkbox, but rather by looping through the entire list. 但是,尽管这会插入正确的Product_ID,但是插入的其余两个值并不对应于选定的复选框,而是通过遍历整个列表。

The question: how do I insert the other 2 values associated to the checkbox into the databse? 问题:如何将与复选框关联的其他2个值插入数据库?

Assuming the Product_ID is unique, you can pass it as the index in all arrays like 假设Product_ID是唯一的,则可以将其作为索引在所有数组中传递,例如

<input type="text" name="product_cost['.$row['Product_ID'].']" value="'.$row['Product_Retail_cost'].'" maxlength="3" size="3" />

And then do something like 然后做类似的事情

foreach($product_ID as $id)
{
  $query="INSERT INTO mjj_ordered_products 
                (`Order_ID`, 
                `Product_ID`, 
                `Product_Quantity`, 
                `Product_Price`)
    VALUES ((SELECT MAX(Order_ID) AS Order_ID FROM `mjj_orders`),
                '".$product_ID[$id]."',
                '".$product_quantity[$id]."',
                '".$product_cost[$id]."')";
    mysql_query($query) or die (mysql_error());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM