I have a form with checkboxes

<input type="checkbox" name="yo[1]" value=1
<input type="checkbox" name="yo[2]" value=1
<input type="checkbox" name="yo[3]" value=1

I want to to insert values into db exactly like this

create table tablename
yo1 tinyint default 0 , yo2 tinyint default 0, yo3 tinyint default 0

insert into table
set yo1=value, yo2=value, yo3=value

How do I do that? (dont want to loop/implode yo[])

/total newbie hoping for some help

5 Years
Discussion Span
Last Post by diafol

Why don't you want to loop yo[]? You created the array
You can use extract() to extract the post data, but it's not encouraged.

I don't think that list() will work for you because on checkboxes checked will send the data, so you have no way of knowing if say $_POST[yo][1] will be sent.

Use a loop?


Please so ad ID property same as your name property to every input item, PHP takes ID i guess.

<input type="checkbox" id="yo1" name="yo1" value="1" />
<input type="checkbox" id="yo2" name="yo2" value="1" />
<input type="checkbox" id="yo3" name="yo3" value="1" />

secondly, it's simple if you do the following:


$mysql_hostname = "your server";
$mysql_username = "your username";
$mysql_password = "your password";
$mysql_dbname = "your database";

//Connecting to Database Server
$con = mysql_connect($GLOBALS['mysql_hostname'], $GLOBALS['mysql_username'], $GLOBALS['mysql_password']) 
or die("Unable to Establish Database Connection.");

//Connecting to Database Schema
mysql_select_db($GLOBALS['mysql_dbname'], $con)
or die("Unable to Connect Database.");

$yo1 = $_POST['yo1'];
$yo2 = $_POST['yo2'];
$yo3 = $_POST['yo3'];

$cmd = mysql_query("INSERT INTO table(yo1, yo2, yo3)
VALUES('$yo1', '$yo2', '$yo3')")
or die ("Error Collectors: " . mysql_error());

// Closing Connection

Hope this is what you were looking for.

Edited by vidjin: n/a


Thanx vidjin but now I dont have an array anymore and as I understand it id is used by css and javascript not php.

Ardav, thank you. Can you tell med how you would do it with loop?

$sql = "";
//set the limits to the number of fields you have in the DB/no of checkboxes (here x = 1 to 3)
 if(isset($_POST['yo'][$x]))$sql .= "`yo{$x}` = 1,"; //this builds up the SQL statement
if($sql != ""){
  $sql = mysql_query("INSERT INTO `table` SET " . substr($sql,0,-1));
   ...//no checkboxes checked

That's my way at the moment. loads of ways in which you could do this.
Also there's no need to clean the form input as you're controlling the DB input.
BUT doing this:

$yo1 = $_POST['yo1'];
$yo2 = $_POST['yo2'];
$yo3 = $_POST['yo3'];

could throw an error if one of the checkboxes isn't checked. In addition because the previous poster didn't clean the input, you'd be open to SQL injection attacks. Anybody could spoof your form and set the value of say, $_POST to anything they wanted.

Edited by diafol: n/a


Thanx ardav .
Have one final question (probably a stupid one)

How come I can't retrive the value useing $yo1 = $_POST']; ?
and is there a way to asign a default value for unchecked checkbox in html?


This: $_POST'] does not exist, but $_POST[1] does.

Anyway, no if a checkbox is not checked that info is not passed at all, it's as if the checkbox did not exist.

did my code not work?

It's set up so that it's scalable. If you add any more checkboxes, the only changes you need to implement are to the DB (obviously) and to $x<4


Tahnx again ardav, yes i got it to work.
You are right about the SQL injection part and it's a good practice but in this case i have a tinyint so the db would only accept INT between -128 and 127 or unsigned 0 to 255. ;)

...again.. thank you for your time!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.