Jump to content

Add data to database - checkboxes


papaface

Recommended Posts

Hello,
I was wondering if someone can help me.
Currently I have the following code in a file named submit.php:
[code]<?php

require("includes/connect.php");
require("includes/selectdb.php");
?>
<form action="process.php" method="post">
<?php
$sql = mysql_query("select id,name from groceries",$con);
while (list($id,$name) = mysql_fetch_array($sql))
{?>
<label><?=$name?><input name="<?=$id?>" type="checkbox" value="<?=$name?>"></label><br>
<?php }
?>
</form>[/code]

How would I go about adding the values of an unlimited amount of checkboxes to a database?
Link to comment
https://forums.phpfreaks.com/topic/30698-add-data-to-database-checkboxes/
Share on other sites

make an array out of it... like so:

[code]
<?php

require("includes/connect.php");
require("includes/selectdb.php");

if(!isset($_POST['submit'])){
?>
<form action="process.php" method="post">
<?php
$sql = mysql_query("select id,name from groceries",$con);
while (list($id,$name) = mysql_fetch_array($sql))
{?>
<label><?=$name?><input name="groceries[]" type="checkbox" value="<?=$name?>"></label><br>
<?php }
?>
<input type="submit" value="Process" name="submit">
</form>
<?php
}else{ //they have submitted the form
$groceries = $_POST['groceries'];

echo "Selected Groceries:<ul>";
for($i = 0; $i < count($groceries); $i++){
echo "<li>".$groceries[$i]."</li>";
}
echo "</ul>";
}
?>
[/code]

of course you can change the echoing out the groceries to making a query out of it... BUt hopefully that is what you are after...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.