Hi, I need help to insert multiple checkbox data into MySQL using PHP.
I'm new here, any code I can refer?

I have a database name called subject_name and there is only 2 column in the table which is id and subject.

I would like to insert multiple checkbox data into the table.

Any help would be greatly appreciated! Thanks!

Member Avatar for rajarajan2017

Checkbox.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CheckBoxes</title>only one working out of list
</head>

<body>
<form id="form1" name="form1" method="post" action="Check.php">
<p>
<label>
<input type="checkbox" name="test[]" value="Bus" id="test_0" />
Bus</label>
<br />
<label>
<input type="checkbox" name="test[]" value="Car" id="test_1" />
Car</label>
<br />
<label>
<input type="checkbox" name="test[]" value="Bike" id="test_2" />
Bike</label>
<br />
<label>
<input type="checkbox" name="test[]" value="Coach" id="test_3" />
Coach</label>
<br />
<label>
<input type="checkbox" name="test[]" value="Taxi" id="test_4" />
Taxi</label>
<br />
</p>
</p>
<p>&nbsp;</p>

<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>
</body>
</html>

Reading array values in php:

<?php
foreach($_POST['test'] as $index => $val){
echo "test[".$index."]=".$val;
}
?>

Here you should use your database code to update the values in the database. Thats it.

Thanks for your reply. It doesn't update to my database..

form.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="testing.php">
<input type="checkbox" name="subject[]" value="WD">Web Design <br />
<input type="checkbox" name="subject[]" value="PH">PHP <br />
<input type="checkbox" name="subject[]" value="MY">Mysql <br />
<input type="checkbox" name="subject[]" value="JV">Java <br />
<input type="checkbox" name="subject[]" value="VB">VB.net <br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

testing.php

<?php //insert to the table
if(isset($_POST['subject'])){
$host="localhost"; // Host name 
$username="*****"; // Mysql username 
$password="*****"; // Mysql password 
$db_name="*****"; // Database name
$tbl_name="test"; // Table name

foreach($_POST['subject'] as $index => $val){
echo "subject[".$index."]=".$val;
}

         

}
?>

The output if all checked:
subject[0]=WDsubject[1]=PHsubject[2]=MYsubject[3]=JVsubject[4]=VB

But it doesn't store into my database...

Member Avatar for rajarajan2017

Where is your update statement, connection code, everything should have to guide you?

is it this you are talking about ?
sorry im new in php :(

<?php //insert to the table
if(isset($_POST['subject'])){
$host="localhost"; // Host name 
$username="***"; // Mysql username 
$password="***"; // Mysql password 
$db_name="***"; // Database name
$tbl_name="test"; // Table name

foreach($_POST['subject'] as $index => $val){
echo "subject[".$index."]=".$val;
}

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$sqlquery = "INSERT INTO $subject VALUES('$id','$subject')";

$results = mysql_query($sqlquery);

mysql_close();
         

}
?>
Member Avatar for rajarajan2017
foreach($_POST['test'] as $index => $val){
$sqlquery = "INSERT INTO $subject VALUES('ival')"; 
$results = mysql_query($sqlquery);
}
foreach($_POST['test'] as $index => $val){
$sqlquery = "INSERT INTO $subject VALUES('ival')";
$results = mysql_query($sqlquery);
}

ival means?

Hi!
First i want to thank all you people who have been a huge help for me, i almost always i find the answers of my questions.

But i would like to know is it possible to retrieve more values from the table, (if the table have 4 columns) how can i catch all of them with select function?

Hi!
First i want to thank all you people who have been a huge help for me, i almost always i find the answers of my questions.

But i would like to know is it possible to retrieve more values from the table, (if the table have 4 columns) how can i catch all of them with select function?

you can use normal select statement to retrieve all the columns.

Select name, address, phone, matricNo from Student

if you wanna to specify the query. Use WHERE

you can use normal select statement to retrieve all the columns.

Select name, address, phone, matricNo from Student

if you wanna to specify the query. Use WHERE

Thanks Lili!
but i am talking about the checkbox values, how can i pass name, address, phone, matricNo threw the checkbox
thanks!

Thanks Lili!
but i am talking about the checkbox values, how can i pass name, address, phone, matricNo threw the checkbox
thanks!

where do you wanna pass the value? from checkbox to database?

you could do exactly like the related post you show to me.
you post will be something like this.
for example:
chckbox[]="John"
chckbox[]="California"

you need to loop this value in order to insert to db accordingly.

you could do exactly like the related post you show to me.
you post will be something like this.
for example:
chckbox[]="John"
chckbox[]="California"

you need to loop this value in order to insert to db accordingly.

Sorry i am not sure if you andestand my quation
id, name, address, phone
1, lili, street2, 00467....
<input type="checkbox" name="information[]" value="Lili">Lili<br /><input>

and i want even to copy the name, id and the adress of this row, not only the phone with the checkbox
thanks!

wrong

What is wrong snu?

Visit this link below
http://www.w3schools.com/php/php_mysql_select.asp

Hi mahavir123!
My problem is not to get the records from the table, not even to print the checkboxes whit values from the that table, an i can even pass the id and one column of each row, but i need to pass more column values

like:
StudentId, name, address, phone, matricNo from Student
i manage to pass the StudentId, name with checkbox, THE quastion is how i pass even address, phone, matricNo from Student to the new table by chosing checkboxes?

Hi!
First i want to thank all you people who have been a huge help for me, i almost always i find the answers of my questions.

But i would like to know is it possible to retrieve more values from the table, (if the table have 4 columns) how can i catch all of them with select function?

Any help please!

Any help please!

i don't understand what did actually you want. :-/
Use select all statement to fetch all data.

i don't understand what did actually you want. :-/
Use select all statement to fetch all data.

Thanks for asking
if i have these valuse from the fist table witch i want to copy by using checkboxes:
StudentId, name, address, phone, matricNo from Student
and insering it into new table
i manage to pass the StudentId, name with checkbox value,
THE quastion is how i pass even address, phone, matricNo from Student to the new table by chosing these values with checkboxes?

Thanks for asking
if i have these valuse from the fist table witch i want to copy by using checkboxes:
StudentId, name, address, phone, matricNo from Student
and insering it into new table
i manage to pass the StudentId, name with checkbox value,
THE quastion is how i pass even address, phone, matricNo from Student to the new table by chosing these values with checkboxes?

You mean something like this. If not, could you please post your code together.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="#" method="get" name="form1">
<input name="name" type="checkbox" value="Lili Edryana" />Lili Edryana
<BR />
<input name="phone" type="checkbox" value="012345678" />012345678
<BR /><input name="submit" type="submit" value="Save" />
</form>
</body>
</html>

Thanks for the help. Almost wright but i need to pass both values with one checkbox
Because they comes from the same row and will be inseted in new table
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.