Page 1 of 1

Write SQL array for multiple checkbox values

Posted: Sun Jan 14, 2007 1:48 am
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]

Posted: Sun Jan 14, 2007 3:07 am
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