【发布时间】:2012-08-28 02:07:47
【问题描述】:
我有下表从数据库中获取数据:
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>';
}
我想做的是将行插入到表格中,确保从两个文本框中插入任何更新的值。插入看起来像这样:
$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());
}
虽然这会插入正确的 Product_ID,但插入的其余两个值并不对应于选中的复选框,而是通过循环遍历整个列表。
问题:如何将与复选框关联的其他 2 个值插入数据库?
【问题讨论】:
-
这可能对您的问题没有帮助,但
mysql_query一直是deprecated。 -
与其手动编写查询,而且您的查询似乎很严肃SQL injection bugs,您是否考虑过使用类似CakePHP 或CodeIgnighter 的框架来简化此操作?
-
@tadman 我正在使用 Netbeans。这不应该像你提到的那样有效吗(不过我会检查一下)?感谢您的提醒,在我在线发布之前,我打算解决 SQL 注入问题。
-
Netbeans 是一个编辑器,而不是一个框架。框架将为您提供大量 PHP 代码,这是构建的基础,而不必从头开始编写每一件事。如果您查看类似的框架,您会发现您很少需要手动编写 SQL。