Write SQL array for multiple checkbox values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

Write SQL array for multiple checkbox values

Post by ripcurlksm »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I found a simple code to pass the values of each checkbox and pass their values onto the next page. Now that the checked values are passed to the next page, how do I write these new checkbox values into the table for each returned value?

Here is the code that prints the form and checkbox and also passes it on to the next page:

Code: Select all

<form name='form1' method='post' action='editsubscriber3.php'>

<?
dbConnect();		
// This gets all of the reports so that the admin can set permissions for individual reports
$sql = "SELECT * FROM emt_report ORDER BY date_year DESC, date_month DESC, company ASC";   
$result = mysql_query($sql) or user_error("mysql error " . mysql_error());
$rank = 1;

while($row = mysql_fetch_assoc($result)) 
{
    $id = $row['id'];

   print("[b]<input name='$id' type='checkbox' value='$id'>[/b]"); 

  $rank++;
}
Here is the second page which pulls each checked value and prints it on the page. What I need to do instead of print is to write to the database the user id and checkbox value for every checkbox. How do I write an SQL array to insert a new row for each checked value? Here is the code for the second page:

Code: Select all

foreach ($_POST as $key => $value ) 
// --- HOW DO I PRINT AN SQL STATEMENT HERE TO WRITE A NEW ROW FOR EACH RETURNED CHECKBOX VALUE? ---
echo "$key <br>";

How do I write the checkbox values into the database? Do I use an array to write an SQL statement for each result? Suggestions? I know it needs to look something like this, but executed each time there is a returned value:

$sql = "INSERT into user_reports VALUES ('$user' , '$key')";


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

To be able to insert multiple values from checkbox, your checkbox name must be in array. Just like
input name='checkboxes[]' type='checkbox' value='$id'>


Just an algorithm

Code: Select all

For each checboxes
    Get Checkbox Value
    Generate Insert Query For Each Check Box Value
    Execute Each Insert Query
End For
Post Reply