[英]PHP and mysql checkbox insert
I am working on a project where i need to pass values (from a list of products) of multiple checkboxes AND text boxes to a table (new_orders) on the database. 我正在一个项目中,我需要将多个复选框和文本框的值(从产品列表中)传递到数据库中的表(new_orders)。
I managed to create the list retrieving the products from the table articles, but i can't insert the products that i choose with the quantities that i insert to the table new_orders on the database. 我设法创建了从表文章中检索产品的列表,但是我无法插入我选择的产品以及插入数据库表new_orders中的数量。
Here is the code: 这是代码:
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();
}
}
}
?>
I ALWAYS get the error message. 我总是收到错误消息。
Single qoutes are missing in 缺少单个qoutes
$insert_order_query= "INSERT INTO new_orders (id_article, quantity, username) VALUES (".$id_article.",".$quantity.", '".$username."')";
try something like 尝试类似
$insert_order_query= "INSERT INTO new_orders (id_article, quantity, username) VALUES ('".$id_article."','".$quantity."', '".$username."')";
but echo that $insert_order_query 但回显$ insert_order_query
paste here what you get - debugging tells a lot:) 在这里粘贴您得到的内容-调试可以说明很多问题:)
EDIT 编辑
and especially do echo your $quantity 特别是呼应您的$ quantity
that is because $quantity=$_POST['quantity_.$id_article'];
那是因为$quantity=$_POST['quantity_.$id_article'];
should be $quantity=$_POST['quantity_'.$id_article];
应该是$quantity=$_POST['quantity_'.$id_article];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.