Get the check box values from database in php
This program teaches you
how to get the multiple checkbox values, dropdown select option values and
radio button values from database using PHP. By using this program we can get
the values into HTML Form from Database. This article is easy to understand and
I hope it will help you.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
include “database_connection.php”;
$sql= mysql_query("select * from studentinfo where studentname”narendar”");
$row = mysql_fetch_array($sql);
$sname=$row[“studentname”];
$fname=$row[“fathername”];
$gender=$row[“gender”];
$sub = explode(“,”,$row[“subjects”]);
$country=$row[“country”];
?>
<form action=""method="post">
<h3 align="center">Edit Information</h3>
<table border="1" cellpadding="2" cellspacing="2" align="center"><tr>
<td>Student Name :</td><td><input type="text" name="sname" value="<?echo $sname;?>"></td></tr>
<tr><td>Father Name:</td><td><input type="text" name="fname" value="<?echo $fname;?>"></td></tr>
<tr><td>Gender:</td><td><input type="radio" name="gender" value="male"
<?php echo ($gender==”male”)?"checked":"" ;?>>Male
<input type="radio" name="gender" value="female"
<?php echo ($gender==”female”)?"checked":"" ;?>>Female</td></tr>
<tr><td>Subjects:</td><td>
<?
if(in_array("maths",$sub))
echo “<input type="checkbox" name="subjects[]" value="maths" checked>Maths <br>”;
else
echo “<input type="checkbox" name="subjects[]" value="maths">Maths <br>”;
if(in_array("science",$sub))
echo “<input type="checkbox" name="subjects[]" value="science"checked>Science <br>”;
else
echo “<input type="checkbox" name="subjects[]" value="science">Science <br>”;
if(in_array("computers",$sub))
echo “<input type="checkbox" name="subjects[]" value="computers"checked>Computers <br>”;
else
echo “<input type="checkbox" name="subjects[]" value="computers">Computers <br>”;
if(in_array("english",$sub))
echo “<input type="checkbox" name="subjects[]" value="english"checked>English “;
else
echo “<input type="checkbox" name="subjects[]" value="english">English”;
?>
</td></tr>
<tr>
<td>Country:</td>
<td>
<select name="country">
<option disabled selected><?echo $country;?></option>
<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="Update"></td>
</tr>
</table>
</form>
</body>
</html>