php - Adding multiple check box values to a database -



php - Adding multiple check box values to a database -

i trying create table user ticks boxes of nutrient have eaten , carb value sent table. contents of table on database phone call code:

while($row = mysqli_fetch_array($q)) { echo "<form action='add.php' method='post' id='add'><tr>"; echo "<td>".$row['carb_id']."</td>"; echo "<td>".$row['food_item']."</td>"; echo "<td>".$row['serving_size']."</td>"; echo "<td>".$row['carbs_per_serving']."</td>"; echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."' name='food[]'></td>"; echo "</tr></form>"; } echo "<input type='submit' class='add'form='add'>";

the php adding other table is:

<?php /*blog.php process basic form saving info */ error_reporting ('e_all'); //create connection database $link = mysqli_connect('localhost','root','','login') or die('error' . mysqli_error($link)); //if there no connection trhows error message if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $food = $_post['food']; //insert values database //mysqli_query($link,"insert blog(post) // values ('".$post."')" // ); //echos out info set in apart password mysqli_query($link,"insert carbsummary(cpp) values ('".$food."')" ); echo $food; ?>

i hope have explained myself here. in short im loocking have user check boxes submit carb value of foods next table.

i not great coding iv not long started, can't seem find solution anywhere.

first:

when creating form, loop create many forms. should alter code:

while($row = mysqli_fetch_array($q)){ echo "<form action='add.php' method='post' id='add'><tr>"; echo "<td>".$row['carb_id']."</td>"; echo "<td>".$row['food_item']."</td>"; echo "<td>".$row['serving_size']."</td>"; echo "<td>".$row['carbs_per_serving']."</td>"; echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."' name='food[]'></td>"; echo "</tr></form>"; } echo "<input type='submit' class='add'form='add'>";

with this:

while($row = mysqli_fetch_array($q)){ echo "<form action='add.php' method='post' id='add'>"; echo "<tr><td>".$row['carb_id']."</td>"; echo "<td>".$row['food_item']."</td>"; echo "<td>".$row['serving_size']."</td>"; echo "<td>".$row['carbs_per_serving']."</td>"; echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."' name='food[]'></td>"; } echo "<input type='submit' class='add'form='add'>"; echo "</tr></form>";

}

second:

$_post['food'] contains arrays returns array, should phone call index value.

$food = $_post['food'][0]; third

if want send many insertion @ same time should alter form with:

echo "<form action='add.php' method='post' id='add'>"; while($row = mysqli_fetch_array($q)){ echo "<tr><td>".$row['carb_id']."</td>"; echo "<td>".$row['food_item']."</td>"; echo "<td>".$row['serving_size']."</td>"; echo "<td>".$row['carbs_per_serving']."</td>"; echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."' name='food[]'></td>"; } echo "</tr>"; } echo "<input type='submit' class='add'form='add'></form>";

and alter query execution with:

foreach($_post["food"] $food){ mysqli_query($link,"insert carbsummary (cpp) values ('{$food}')"; echo $food; }

php html mysql table checkbox

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -