I've searched extensively but can't find an answer… Hope someone can help:
I'm a newbie PHP and MySQL user and have a problem with checkboxes.
I have a simple HTML page which contains checkboxes. The page is linked up to a MySQL db in PHPmyadmin.
The HTML is:
<html><p>User1<input type="checkbox" name="Users[]" id="Users1" value="1"/></p>
<p>User2<input type="checkbox" name="Users[]" id="Users2" value="2"/></p>
<p>User3<input type="checkbox" name="Users[]" id="Users3" value="3"/></p>
<p>User4<input type=开发者_开发百科"checkbox" name="Users[]" id="Users4" value="4"/></p></html>
What I want is for the person filling in the form to check 1 or more of the values and then for the checked values to be displayed in PHPmyadmin, so that I can export them.
However, when using this PHP:
$values = implode(',', $_POST['Users']);
All I get in PHPmyadmin is "Array", and I can't figure out how to get the actual values to be displayed.
Thanks in advance,
You can get all value checked by looping :
$userChecked = $_POST['Users'];
for ($i=0; $i<count($userChecked ); $i++) {
echo( ($i+1) . ") " . $userChecked [$i] . "<br/>");
}
This will display all id. Instead of echo the value you can insert them in your database or do what you want. The values will be loop inside : $userChecked [$i]
@Daok: Yes, that displays the values on the results page, but in PHPmyadmin, it still indicates "Array". How do I get it to display the values?
PhpMyAdmin is just a tool to administrate php/mysql. I guess you mean that you have "array" written in your database? If you do want all value inside a field (varchar) than you just have to implode
:
$comma_separated = implode(",", $_POST['Users']);
//Code here to Insert to you database with ...value($comma_separated)...
If you want 1 row for each entry :
$userChecked = $_POST['Users'];
for ($i=0; $i<count($userChecked ); $i++) {
//Insert Sql statement here with value($userChecked [$i])
}
try
$values = implode(',', (array)$_POST['Users']);
or
Users=array();
$values = implode(',', $_POST['Users']);
精彩评论