I have a checkbox of titles of products and a text box for each to fill in with quantity. I'm trying to insert into my database the data given in checkbox and textbox for each entry. This below seems to work but it worked well only when I check in the checkbox the first entry of the titles given.
---> Ellhnikos-x2 Record inserted
During the next trials for ordering products, only the products where printed and my database obviously was not fully updated. ---> Freddoccino -x Record inserted
My php code is below: (addorder.php)
<?php
$conn = mysql_connect("localhost", "root", "");
$db=mysql_select_db("buzzcafe" ,$conn);
db_connect();
if (isset($_POST['products'])) {
if (isset($_POST['quantity'])) {
foreach($_POST['products'] as $key => $products){
$quantity = isset($_POST['quantity'][$key])? $_POST['quantity'][$key]:'Not selected';
// $quantity = isset($_POST['quantity']);
$message[] = $products.' - x'.$quantity;
$insertOrder = mysql_query("INSERT INTO orders (title, quantity) VALUES('".$products."','".$quantity."')")or die(mysql_error());
}
echo implode(',', $message);
echo "<br />Record inserted";
}
else { echo "You did not choose a quantity."; }
}else { echo "You did not choose any product."; }
And the code for my addorder_form.php is as follows:
<?php
db_connect();
$q = 0;
$cats=array("Coffee", "Beverages", "Drinks", "Snacks/Desserts");
$arrlength=count($cats);
echo '<form method="post" action="addorder.php">';
for($i=0;$i<$arrlength;$i++) {
$sql = mysql_query('SELECT title FROM products WHERE cname="'.$cats[$i].'"') or die(mysql_error());
echo "<h4 style=color:#800000> ".$cats[$i]."</h4>";
echo "<br />";
$login_check_products = mysql_num_rows($sql);
while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
echo '<tr><td></td><td>';
echo "<input style='text-align:right;' type='checkbox' action='addorder.php' name='products[]' value='".$row["title"]."'>".$row["title"];
echo '</td></tr><tr><td></td><td>';
echo "<input type='text' size='4' action='addorder.php' name='quantity[]'>";
echo '</td></tr><br />';
}
}
echo '<tr><td colspan="2" align="center" ><input type="submit" value="Submit Order"/></td></tr><tr></form>';
?>
How can I make it correct?