lundi 26 janvier 2015

Adding rows to database based on selected checkbox and quantities - PHP

My script selects a list of items from a db table and presents them alongside a checkbox. If the checkbox is checked and the form is submitted, that item is added into the 'stock' table.


I have had to make an adjustment so that the user can select a checkbox and also chose a quantity. However, I am not using 'quantity' as a value to be inserted into a row. Instead, if a user selects a quantity of 3 for stock item 'Apple', I would want 3 rows of Apple to be added to the database. My current HTML script (wrapped in a while loop):



<label><input type="checkbox" value="<?php echo $show3['Part_Type']; ?>,<?php echo $show3['Sub_Type']; ?>" class="cbgroup1" name="cbg1[]" />
<?php echo $show3['Sub_Type']; ?><input type="number" size="2" value="1" name="qty_<?php echo $show3['Sub_Type']; ?>"></label>


The script for posting to the db:



foreach ($_POST['cbg1'] as $stock) {
$lst = explode(",", $stock);
//I do this because I send two values through the checkbox separated by a comma

$query = ("INSERT INTO stock SET Item_Specifics_Type='$lst[0]',
Item_Specifics_SubType='$lst[1]',
colour = '$colour',
additional='$additional',
stock_item = '$Make $ModelName $lst[1]'
");
$result = mysql_query($query) or die(mysql_error());
}




Aucun commentaire:

Enregistrer un commentaire