1

i have this code which permits me to retrieve data from a checkbox, there isn't any mysql included yet, so can you help me modify this?

The form is like this:

<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="hostess_name">
<input type="checkbox" name="checkbox[]" value="hostess_familyname_en">
<input type="checkbox" name="checkbox[]" value="hostess_id">
<input type="checkbox" name="checkbox[]" value="hostess_firstname_en">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>

The values i inserted are supposed to be database fields, then i have this checkbox.php file which reads the values selected.

    <?php
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
<?

/* and in your checkbox.php you do this: */

if(isset($_POST['Submit']))
{
echo "<pre>"; print_r($_POST);
}
$checked = mysql_real_escape_string(implode(',', $_POST['checkbox'])); 
echo $checked;
?>

How can assign to checkbox values database fields? What i'm trying to do is to store the values and export them into a query using implode.. Help me..

4
  • Start with var_dump($_POST) and go from there. Commented Jun 18, 2012 at 19:07
  • can you please tell me how to store the values in an array, i need to implode the array then... Commented Jun 18, 2012 at 19:10
  • This looks very similar to this question: stackoverflow.com/questions/11081297/… Commented Jun 18, 2012 at 19:10
  • Yes, we are building this script together, is just that i found another way to do that. more code inserted but at least i can understand it.. Commented Jun 18, 2012 at 19:12

2 Answers 2

2

Your POST variable ($_POST['checkbox']) is actually already an array. First, to figure out what you are actually working with, do this:

echo '<pre>';
print_r ($_POST['checkbox']);
echo '</pre>';

Then view your script and have a look at the output. Chances are you'll see an array with some keys and values. Using that you can decide how to proceed.

If it were me I would do something like the following to accomplish your task:

$sql = "SELECT `table_id_column`, `another_column` ";
foreach ($_POST['checkbox'] as $key => $value) {
  $sql .= ", `$value`";
}
$sql .= " FROM `hostess` ORDER BY `another_colmn` ASC";

Please keep in mind that allowing an SQL statement to be modified in this manner is very bad practice. You'll want to introduce some security into this before putting it on a production environment.

Luke

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks, it gives me an error like this : Warning: Invalid argument supplied for foreach() Please can you see the code i've updated, if i can do it that way..it would be easier for me..
Your code is correct so far. You'll simply need to include that variable in an SQL statement like so: $sql = "SELECT $checked FROM hostess" ... please be very careful with this though as it is full of security holes.
1

Use

foreach($_POST[checkbox] as $key => $value {
    echo PHP_EOL . "Key is => " . $key . " Value is => " . $value . PHP_EOL;
}

Try this and see the output, you'll see yourself how to proceed further.

10 Comments

remove the [] from your form and try
or just get rid of the foreach loop, and simply do this.. echo "<pre>"; print_r($_POST); and see what values are being stored in the $_POST array. Itll give you a better idea.
It still gives me that error, maybe i'm doing something wrong, can you help me with just this part, i need to save the checkbox values in an array so i can do implode then..
If i do that, it prints me an array.. but how can i do the implode part here... i mean i need to make a query like this : Select $variable from hostess etc etc
once you get the array, notice its form... then loop over each element, and do whatever you want.. How exactly do you want to implode them? Using what delimeter/char/glue?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.