How To: Insert Data into Database using Check Box in PHP
Here i am going to insert data into mysql database using checkbox in php script. In radio button, only one option have to be selected under many option and all that options name will be same. In Checkbox also same as the radio button’s properties, but the difference between them is many options can be selected in checkbox. So that name of check box is to be given in array ( game[])
Create database ”data” and table “tb”.
1 2 3 4 5 6 7 |
CREATE TABLE `data`.`tb` (`id` VARCHAR( 25 ) NOT NULL , `choice` VARCHAR( 5 ) NOT NULL) ENGINE = INNODB; |
Here $_POST['game'] is a array value, so we have to implode each variable with thecomma (i.e., seperate the selected variables with comma ) and store in database.
If four values hockey, volleyball are selected, then the data that store in databse is hockey, volleyball
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<html>
<body>
<?php
if(isset($_POST['submit']))
{
$choice=$_POST['game'];
$choice1=implode(',',$choice);
echo $choice1;
mysql_query("insert into tb values("","$choice1");
}
?>
<fieldset>
<form method="post" action="index.php">
Select your favourite game:
Hockey:<input type="checkbox" name="game[]" value="hockey">
Cricket:<input type="checkbox" name="game[]" value="cricket">
Basketball:<input type="checkbox" name="game[]" value="basketball">
Volley ball<input type="checkbox" name="game[]" value="Volley ball">
<input type="submit" name="submit">
</form>
</fieldset>
</body>
</html>
|




