作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个项目,我需要将多个复选框和文本框的值(来自产品列表)传递到数据库上的表(new_orders)。
我成功创建了从表articles中检索产品的列表,但我无法插入我选择的产品以及插入数据库中的new_orders表的数量。
这是代码:
foreach($A_result as $key => $value) // για χρηση όλων των αντικειμένων του πίνακα A_apotelesma
{
// shows al the products
echo "<tr>
<td align=center>".$j++."</td>
<td align=center>".$value['name']."</td>
<td align=center>".$value['price']."</td> ";
echo"<td> // each product has a checkbox and a text box to enter the quantity
<input type=checkbox name=\"article[]\"></input>
<input type=text name='quantity_".$value['id_article']."' size='3' maxlength='2'>
</td>";
echo"<td><input type='hidden' name='id_article' value=".$value['id_article']."></td>";
echo"<td><input type='hidden' name=\"code_user\" value=\"code_user\"></td>";
echo"</tr>";
}
}
?>
// The form above redirects to the insert_order.php page:
<?php
include("conn.php");
session_start();
$checkbx=$_POST['article'];
if($_POST['article']){
for($i=0;$i<sizeof($checkbx);$i++){
$quantity=$_POST['quantity_.$id_article'];
$username=$_SESSION['logged_user_username'];
$insert_order_query= "INSERT INTO new_orders (id_article, quantity, username) VALUES (".$id_article.",".$quantity.", '".$username."')";
//echo $insert_order_query;
$insert_order=mysql_query($insert_order_query) or die('Error,query failed!!');
if ($insert_order)
echo '<script language="javascript">alert("New order created!"); document.location="logged_in_user.php?menu=1";</script>';
else
{
echo '<script language="javascript">alert("The order has not been created.")</script>';
echo '<script language="javascript"> document.location="logged_in_user.php?menu=1.php"; </script>';
exit();
}
}
}
?>
我总是收到错误消息。
最佳答案
那是因为 $quantity=$_POST['quantity_.$id_article'];
应该 $quantity=$_POST['quantity_'.$id_article];
关于PHP 和 mysql 复选框插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22459295/
我是一名优秀的程序员,十分优秀!