Forum Moderators: coopster

Message Too Old, No Replies

Entering mutiple value with checkbox into mysql

Entering mutiple value with checkbox into mysql

         

s4rkx

9:41 am on Jul 29, 2004 (gmt 0)

10+ Year Member



My form only seems to select one value when i may have selected 1 or more values from checkbox.

i want to be able to insert none or all selected from checkbox into mysql database.

please help

<tr>
<td>&nbsp;</td>
<td>Ethnic Group</td>
<td colspan="2"><table width="95%" border="0">
<tr>
<td width="6%"><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="Asian Other"></td>
<td width="44%">Asian (Other)</td>
<td width="8%"><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="Indian"></td>
<td width="42%">Indian</td>
</tr>
<tr>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="Bangladeshi"></td>
<td>Bangladeshi</td>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="Pakistani"></td>
<td>Pakistani</td>
</tr>
<tr>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="Black African"></td>
<td>Black African</td>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="Vietnamese"></td>
<td>Vietnamese</td>
</tr>
<tr>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="Black African-Caribbean"></td>
<td>Black African-Caribbean</td>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="White Irish"></td>
<td>White Irish</td>
</tr>
<tr>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="Black British"></td>
<td>Black British</td>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="White Other"></td>
<td>White Other</td>
</tr>
<tr>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="Chinese"></td>
<td>Chinese</td>
<td><input name="ethnicgroup" type="checkbox" id="ethnicgroup" value="White UK"></td>
<td>White UK</td>
</tr>

</table>
</form>
<?

include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('$ethnicgroup')";
mysql_query($query);

echo "Record $ethnicgroup Inserted";

mysql_close();

?>
<p><br>

RonPK

10:02 am on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi & welcome,

Change all occurences of name="ethnicgroup" into name="ethnicgroup[]". That way ethnicgroup will be available as an array on the server.

As you cannot store arrays in a database field, you'll have to use the implode() function to make a string out of the ethnicgroup array.

coopster

11:57 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



change all occurences of name="ethnicgroup" into name="ethnicgroup[]". That way ethnicgroup will be available as an array on the server.

Right on, RonPK, and then you refer to them without the brackets [php.net] in the $_POST variable. And actually, you can store arrays in a database field if you serialize [php.net] them.


$array = serialize($_POST['ethnicgroup']);
mysql_query("INSERT INTO table VALUES('$array')");
It's a pretty common concept when you implement your own session handling.

dreamcatcher

9:25 am on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And actually, you can store arrays in a database field if you serialize them.

Wow, thanks for that Coop. Another useful snippet of code for my code bank. :)

s4rkx

12:57 pm on Jul 30, 2004 (gmt 0)

10+ Year Member



Im probably being thick, have tried your solution, am sure it will work, but when entering details i get

N; showing in my database

tr>
<td width="6%"><input name="ethnicgroup[]" id="ethnicgroup[]" type="checkbox" value="Asian Other"></td>
<td width="44%">Asian (Other)</td>
<td width="8%"><input name="ethnicgroup[]" id="ethnicgroup[]" type="checkbox" value="Indian"></td>
<td width="42%">Indian</td>
</tr>
<tr>
<td><input name="ethnicgroup[]" id="ethnicgroup[]" type="checkbox" value="Bangladeshi"></td>
<td>Bangladeshi</td>
<td><input name="ethnicgroup[]" id="ethnicgroup[]" type="checkbox" value="Pakistani"></td>
<td>P

lude("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
//if (isset($_POST['Submit'])) {
$ethnicarray = serialize($_POST['ethnicgroup']);
$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web','$orgname',
'$orgadd1','$orgadd2','$orgpostcode','$orgcounty','$ward','$client_group',
'$district','$ethnicarray','$disability','$areaofwork')";
mysql_query($query);

As very grateful for help

[edited by: coopster at 2:45 pm (utc) on July 30, 2004]
[edit reason] fixed sidescroll [/edit]

RonPK

1:58 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



s4rkx, parts of your posting seem to be lost, including the part where you mention what
happened in the database. Could you re-post that part?

Also, but that should not be causing SQL problems: in your HTML, id-s should be unique.
You can't have several id="ethnicgroup[]" , so maybe it's easiest to number them like id="ethnicgroup3".

s4rkx

2:12 pm on Jul 30, 2004 (gmt 0)

10+ Year Member



This is most of the code, should be abel to get jist, everything works except the checkbox selections, i need checkboxes in three sections, so have been experimenting, its taken hours....

<tr>
<td>&nbsp;</td>
<td>District</td>
<td><select name="district" id="district">
<option value="Please Select" selected>Please Select</option>
</select></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Ethnic Group</td>
<td colspan="2"><table width="95%" border="0">
<tr>
<td width="6%"><input name="ethnicgroup[]" id="ethnicgroup1" type="checkbox" value="Asian Other"></td>
<td width="44%">Asian (Other)</td>
<td width="8%"><input name="ethnicgroup[]" id="ethnicgroup2" type="checkbox" value="Indian"></td>
<td width="42%">Indian</td>
</tr>
<trimmed repetitive code>
</table></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Disability</td>
<td colspan="2"><table width="95%" border="0">
<tr>
<td width="6%"><input name="disability" type="checkbox" id="disability[]" value="Hearing Impaired"></td>
<td width="41%">Hearing Impaired</td>
<td><input name="disability" type="checkbox" id="disability[]" value="Physical Disabilities"></td>
<td>Physical Disabilities</td>
</tr>
<trimmed repetitive code>
</table></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Area of Wrok</td>
<td colspan="2"><table width="95%" border="0">
<tr>
<td width="6%"><input name="areaofwork" type="checkbox" id="areaofwork[]" value="Advise / Advocacy"></td>
<td width="41%">Advise / Advocacy</td>
<td><input name="areaofwork" type="checkbox" id="areaofwork[]" value="Counselling"></td>
<td>Counselling</td>
</tr>
<trimmed repetitive code>
</table></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input name="Submit" type="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

<?
include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$ethnicarray = serialize($_POST['ethnicgroup']);
$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web','$orgname',
'$orgadd1','$orgadd2','$orgpostcode','$orgcounty','$ward','$client_group',
'$district','$ethnicarray','$disability','$areaofwork')";
mysql_query($query);
echo "Record $first Inserted";
mysql_close();

?>

[edited by: jatar_k at 4:58 pm (utc) on July 30, 2004]
[edit reason] fixed sidescroll - trimmed code [/edit]

RonPK

2:25 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but when entering details i get

N; showing in my database

That's the part in your previous posting where I think something went wrong, and which may contain relevant information.
Exactly what did you see in the database?

s4rkx

3:52 pm on Jul 30, 2004 (gmt 0)

10+ Year Member



when i goto mysqladmin, and select a search for all entries in db, this is what i see, N;

Ths thing is im using post so i can see the url,

this is what i get:

[domain.com...]
last=&phone=&mobile=&fax=&email=&web=&orgname=&orgadd1=&
orgadd2=&orgcounty=&orgpostcode=&ward=Please+Select&
client_group=Please+Select&district=Please+Select&
ethnicgroup%5B%5D=Asian+Other&ethnicgroup%5B%5D=Bangladeshi&
ethnicgroup%5B%5D=Black+African&
ethnicgroup%5B%5D=Black+African-Caribbean&Submit=Submit+Query

[edited by: jatar_k at 4:53 pm (utc) on July 30, 2004]
[edit reason] broke url to fix sidescroll [/edit]

RonPK

4:45 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please, not all of us have 30" monitors, and I hate to scroll.

Anyway, if you're wondering whether something went wrong with the query, try using this instead of just mysql_query($query) :

mysql_query($query) or die("query: $query<br>" . mysql_error());

That way you will see the actual query, and the error mysql generated. Makes debugging a lot easier.