View Full Version : How to insert checkbox values to mysql using php


tony007
02-21-2006, 05:41 PM
Hi all i got a form with a few checkbox on it. User select a few of checkboxes and and press a button to write their values to db. But i do not know how to write it to mysql db. I be happy if an expert help me with that.Thanks

Note:Assuming the table has one columns which is for id value
code for check box form


<form action="./write.php" method=post >

<input type="checkbox" name="id" value="1"
<input type="checkbox" name="id" value="2"
<input type="checkbox" name="id" value="3"

<input type="submit" value="Add this/these Songs to my PlayList" name="B1">

Dreamweaver
05-27-2007, 04:52 PM
First You'll need to connect to the MySQL Database before you can even think of adding an update Query.
If Ya Search The MySQL Forums I'm sure You'll find what ya need to get ya started.

Mick

Vincenzo
09-05-2007, 08:46 AM
If you're planning of allowing multiple boxes to be checked, you should give each boxes their own unique "name" and each name must have their own column in your db table.

Example:

<input type="checkbox" name="ckbx1" value="">
<input type="checkbox" name="ckbx2" value="">
<input type="checkbox" name="ckbx3" value="">

Then in your PHP code, you should run a check for each boxes by giving them value before updating your database.

Example:

if (ckbx1 != NULL) {
ckbx1 = 1;
}
else {
ckbx1 = 0;
}


Then in your database, the columns ckbx1, ckbx2, and ckbx3 will either have a 0 or a 1, where 0 is unchecked and 1 is checked.

If you choose to limit the choices to just one choice rather than multiple choices, try using a drop-down selection instead.

Example:

<select name="id">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

I hope that helps.