Jump to content

Recommended Posts

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]
<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++;
}[/code]

Here is the second page which pulls each checked value and prints it on the page. [B]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? [/B]Here is the code for the second page:
[code]
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>";
[/code]


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')";
Well the first thing I see that's probably wrong is that in your checkbox element your name and your value is the same. Is that what it's really supposed to be, or do you mean for the checkbox value to be some other piece of data associated with the id? If that's what you are really meaning for it to do, then please further explain why.

as far as your query string:  unless your table only has 2 columns and they are in the correct $user, $key order, you must specifically state your column names in your insert:

[code]
foreach ($_POST as $key => $value ) {
  $sql = "insert into user_reports (userid, somecolumn) values ('$key','$value')";
  $result = mysql_query($sql) or die(mysql_error());
}
[/code]
The $key represents the name of your input element. Since you specified it as $id, that's probably your userid column.  Again, I think you probably meant for the value of your element to be some other piece of data, as it doesn't really make sense to insert the same bit of data (id, id) in 2 seperate columns, so I just named your other column 'somecolumn' in the query, and the $value in the foreach represents whatever data it is supposed to be.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.