Vous êtes sur la page 1sur 7

Insert Value From CheckBox In Database

(MySQL) In PHP

 Vineet Kumar Saini

 Apr 15 2012

 Article

 2
 8
 533k

 facebook

 twitter

 lin kedIn

 google Plus

 Reddit


o
o
o
o
 Expand

Easily Add PDF Word & Excel Function to Your .NET Apps

In the last article you saw how to insert a value from a radio button in the MySQL database in PHP.
Now in this article you will see how to insert a value from a checkbox in the MySQL database. Using
a checkbox you can insert multiple values in the MySQL database in PHP.

First of all we will create a database and a table in MySQL.

Create Database
Create Table

Create config.php file

Now we will create a config.php file for connecting the database to all PHP files.
Create the form.php file

Now we will create a form.php file by which we will insert a value through checkboxes in the MySQL
database.

Create checkbox.php file

No we will create a PHP file in which we include a config.php file and write the insert command for
inserting the data in the database.

Output

For running the above code we will write in the web browser "http://localhost/foldername/form.php" .
Now we select any checkbox. Suppose we select four checkboxes i.e. Vineet Saini, Ravi Sharma,
Rahul Dube, Priyanka Sachan. Then we will click on the submit button.

When we click on the submit button then you will get a message i.e. Record is inserted.
Now you will see there are four records inserted in the database. Like as in the following image.

Conclusion

So in this article you saw how to insert multiple records in a MySQL database through a checkbox.
Using this article one can easily understand insertion of multiple data in a MySQL database.
You should specify

<input type="checkbox" name="Days[]"


value="Daily">Daily<br>
as array.

Add [] to all names Days and work at php with


this like an array.
After it, you can INSERT values at different
columns at db, or use implode and save values
into one column.

Didn't tested it, but you can try like this. Don't
forget to replace mysql with mysqli.
<html>
<body>
<form method="post" action="chk123.php">
Flights on: <br/>
<input type="checkbox" name="Days[]"
value="Daily">Daily<br>
<input type="checkbox" name="Days[]"
value="Sunday">Sunday<br>
<input type="checkbox" name="Days[]"
value="Monday">Monday<br>
<input type="checkbox" name="Days[]"
value="Tuesday">Tuesday <br>
<input type="checkbox" name="Days[]"
value="Wednesday">Wednesday<br>
<input type="checkbox" name="Days[]"
value="Thursday">Thursday <br>
<input type="checkbox" name="Days[]"
value="Friday">Friday<br>
<input type="checkbox" name="Days[]"
value="Saturday">Saturday <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

<?php

// Make a MySQL Connection


mysql_connect("localhost", "root", "") or
die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$checkBox = implode(',', $_POST['Days']);

if(isset($_POST['submit']))
{
$query="INSERT INTO example (orange) VALUES
('" . $checkBox . "')";

mysql_query($query) or die (mysql_error() );

echo "Complete";

?>
shareimprove this answer edited Nov 24 '13 at 16:57 answered Nov 24 '13 at 16:07

Viacheslav Kondratiuk
3,33162962
Do you mind kindly showing me this in code? – SirBT Nov 24 '13 at 16:3

Check my update – Viacheslav Kondratiuk Nov 24 '13 at 17:10

Wonderful! I checked it and it saves several days in one column. How do


instead? – SirBT Nov 24 '13 at 17:36

INSERT INTO example (column1, column2, ...) VALUES ($_


$_POST['Days'][1], ...) – Viacheslav Kondratiuk Nov 24 '13 at 1

Vous aimerez peut-être aussi