douliang2087 2012-08-27 17:41
浏览 69
已采纳

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

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.

The question: how do I insert the other 2 values associated to the checkbox into the databse?

  • 写回答

1条回答 默认 最新

  • dongmu9253 2012-08-27 18:01
    关注

    Assuming the Product_ID is unique, you can pass it as the index in all arrays like

    <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());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改