an array is taking all inputs in php and i want only the checked rows

an array is taking all inputs in php and i want only the checked rows



i want to insert into database several items with its quantity
like name of food and its quantity using checkbox that handle the value of the food name.


here is the code:


<?php ........
if(mysqli_num_rows($res) > 0)
while($row = mysqli_fetch_array($res))
echo "<tr>
<td><input type='checkbox' value='$row[0]' name='food'></td>
<td>
$row[0]
Qty: <input type='number' value='1' size='1' name='qty' id='qty'>
</td>
<td align=right>$row[3]</td>
</tr>";


?>

<input type='submit' value='Add checked' class='btn btnSearch' name='add' id='btnadd'>

</form>



action:


<?php
if(isset($_REQUEST['add']), $_REQUEST['food'])

$food = $_POST['food'];
$qty = $_POST['qty'];

$array = array_combine($food, $qty);

foreach($array as $f => $q)
$sql = "insert into entry (foodName,qty, meal_name, date, email) values ('$f', '$q' ,'$meal', '$date', '$email')";
mysqli_query($cn, $sql);


if(mysqli_affected_rows($cn) > 0)
header("location:fitness.php");
else
echo $sql;

?>



the problem is .. array $qty is taking all inputs of the table not just the checked.

and im getting the error:


Warning: array_combine(): Both parameters should have an equal number of elements in D:ABCFirst StageFitnessAddictAddFood.php on line 93





Yes, that's how it should work, nothing in your code is telling it to disregard any of the qty inputs. Associate the checkbox and number inputs together using the same key and remove all the keys from qty that don't exist in food.
– Devon
Aug 29 at 17:38






What you want is a little complicated. But basically you are sending two different count rows. If you start with 30 rows...and you check 5, you get 5 food rows and 30 qty rows. That's why it doesn't work. You can't do it that way
– Forbs
Aug 29 at 17:56





@Devon can u write ur idea? i didnt get it
– isso kd
Aug 29 at 18:14





@Forbs so whats the solution :/
– isso kd
Aug 29 at 18:14





i was reading ur old comment. hehe ok i will try another thing
– isso kd
Aug 29 at 18:21




2 Answers
2



To give you a better answer change this.


<td><input type='checkbox' value='$row[0]' name='food'></td>



to


<td><input type='hidden' value='$row[0]' name='food2'><input type='checkbox' value='$row[0]' name='food'></td>



then



in your code


foreach($_POST['food2'] as $key=>$val)

if (in_array($val,$food,true))
$array[$val] = $qty[$key];



That should work



So when you check a box in food, it compares the master list in food2 and says here's the matching key you can use for qty.



The issue is that the checkbox type only passes the checked values, and since you are using an array, you are losing the 'key' to reference qty.





i got a Warning: array_combine() expects parameter 2 to be array, string given
– isso kd
Aug 29 at 18:40





oh sorry, I'll fix that
– Forbs
Aug 29 at 18:52





can i use it like foreach($array as $f => $q) .. ?? cz it didnt work like this :/
– isso kd
Aug 29 at 19:53





ok fixed one more time
– Forbs
Aug 29 at 20:25





finally! it works
– isso kd
Aug 29 at 20:29



This will give you an idea..to save


<?php
if(isset($_REQUEST['add']),$_REQUEST['food'])
$food = $_POST['food'];
$qty = $_POST['qty'];
$foodcount = count($qty);
$i=0;]
foreach($qty as $quantity)
If(isset($food[$i]) && !empty($food[$i]))

$sql = "insert into entry (foodName,qty, meal_name, date, email) values ('$food[$i]', '$quantity' ,'$meal', '$date'


if(mysqli_affected_rows($cn) > 0)
header("location:fitness.php");
else echo $sql;
?>





That won't work because you lose the $i in food when you combine a checkbox with an array
– Forbs
Aug 29 at 18:29


food



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế