Forum Moderators: coopster
i want to be able to insert none or all selected from checkbox into mysql database.
please help
<tr>
<td> </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>
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.
It's a pretty common concept when you implement your own session handling.
$array = serialize($_POST['ethnicgroup']);
mysql_query("INSERT INTO table VALUES('$array')");
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]
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".
<tr>
<td> </td>
<td>District</td>
<td><select name="district" id="district">
<option value="Please Select" selected>Please Select</option>
</select></td>
<td> </td>
</tr>
<tr>
<td> </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> </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> </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> </td>
<td> </td>
<td><input name="Submit" type="Submit"></td>
<td> </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]
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ðnicgroup%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]
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.