Insert Checkbox Values into MySQL Using PHP
This article teaches you how
to insert HTML form multiple checkbox values,dropdown select option values and
radio button values into MySQL database using PHP. This article is easy to
understand and I hope it will help you.
<html>
<head>
<title>Student Registration</title>
</head>
<body>
<?php
include "database_connection.php";
if(isset($_POST["submit"])){
$sname = $_POST["sname"];
$fname = $_POST["fname"];
$gender = $_POST["gender"];
$subjects = implode(",",$_POST["subjects"]);
$country = $_POST["country"];
$sql = "INSERT INTO `studentinfo`(`studentname`,`fathername`,`gender`,`subjects`,`country`)
values("$sname","$fname","$gender","$subjects","$country")";
$query = mysql_query($sql) or die(mysql_error());
if
($query){
echo "<h1>Data inserted successful.....</h1>";
}else{
echo "<h1>Please Try again.....</h1>";
}
}
?>
<form name="registration" method="post" action="">
<table border="1" cellpadding="2" cellspacing="2" align="center">
<tr>
<td>Student Name :</td><td><input type="text" name="sname" required=""></td>
</tr>
<tr>
<td>Father Name:</td><td><input type="text" name="fname" required=""></td>
</tr>
<tr>
<td>Gender:</td><td><input type="radio" name="gender" value="male" required="">Male
<input type="radio" name="gender" value="female" >Female</td></tr>
<tr>
<td>Subjects:</td><td><input type="checkbox" name="subjects[]" value="maths">Maths <br>
<input type="checkbox" name="subjects[]" value="science">Science <br>
<input type="checkbox" name="subjects[]" value="computers">Computers <br>
<input type="checkbox" name="subjects[]" value="english">English </td></tr>
<tr><td>Country:</td>
<td><select name="country">
<option >----select----</option>
<option>India</option>
<option>Japan</option>
<option>USA</option>
</select></td></tr>
<tr><td></td>
<td><input type="submit" name="submit" value="Register"></td>
</tr>
</table>
</form>
</body>
</html>














