Hi everyone,
I'm having problem with displaying records that are checked using checkboxes. Here are my codes:
This code is the form to add data into the database:
<?php
include('dbconn.php');
?>
<html>
<head>
</head>
<body>
<script language="Javascript">
function checkForm(form)
{
if(form.firstname.value=="")
{
alert("Please enter a First Name!!");
form.firstname.focus();
return(false);
}
if(form.lastname.value=="")
{
alert("Please enter a Last Name!!");
form.lastname.focus();
return(false);
}
}
</script>
<form name="form" action="profile_add2db.php" method="POST" onSubmit="return checkForm(this)">
<table border='0'>
<tr>
<td>
<b>First Name* :</b> <br>
<input type="text" name="firstname" value="<?php print($firstname);?>" />
</td>
<td>
<b>Last Name* :</b> <br>
<input type="text" name="lastname" value="<?php print($lastname);?>" />
</td>
</tr>
<tr>
<td>
<b>E-Mail Address:</b> <br>
<input type="text" name="email" value="<?php print($email);?>" />
</td>
</tr>
<tr>
<td>
<b>Biography:</b> <br>
<TEXTAREA name="biography" class="biography"><?php print($biography);?></TEXTAREA></td>
</tr>
<tr>
<td>
<b>Practice Areas:</b> <br>
<input type="checkbox" name="checkbox[]" value="business" />Business<br>
<input type="checkbox" name="checkbox[]" value="litigation"/>Litigation<br>
<input type="checkbox" name="checkbox[]" value="landuse"/>Land Use<br>
<input type="checkbox" name="checkbox[]" value="realestate"/>Real Estate<br>
<input type="checkbox" name="checkbox[]" value="seniorlaw"/>Senior Law<br>
<input type="checkbox" name="checkbox[]" value="estates"/>Estates<br>
<input type="checkbox" name="checkbox[]" value="pi"/>P.I.<br>
<input type="checkbox" name="checkbox[]" value="employment"/>Employment<br>
<input type="checkbox" name="checkbox[]" value="family"/>Family<br>
<input type="checkbox" name="checkbox[]" value="criminal"/>Criminal<br>
<input type="checkbox" name="checkbox[]" value="ll/t"/>LL/T<br>
</tr>
</table>
<br>
<input type="submit" value="Add" id="submit" name="submit" />
</form>
</body>
</html>
This code inserts the values to the database:
<?php
include('dbconn.php');
function es($val) {
return mysql_real_escape_string($val);
}
$sql = sprintf("INSERT INTO attorney_profiles(firstname,lastname,email,biography,practice_areas)
VALUES
('%s','%s','%s','%s','%s')",
es($_REQUEST['firstname']),
es($_REQUEST['lastname']),
es($_REQUEST['email']),
es($_REQUEST['biography']),
es($_REQUEST['practice_areas']));
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
{
header("Location: index.php");
echo "1 record added";
}
if(!$sql)
{
echo "No data.";
}
?>
and the following code displays the data:
<?php
include('dbconn.php');
$sql = "SELECT * FROM attorney_profiles";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Click on the name below to update profile.";
echo "<br/>";
echo "<br/>";
$result = mysql_query("SELECT * FROM attorney_profiles");
$num=mysql_num_rows($result);
mysql_close();
$i=0;
while ($i < $num) {
$attorney_id=mysql_result($result,$i,"attorney_id");
$firstname=mysql_result($result,$i,"firstname");
$lastname=mysql_result($result,$i,"lastname");
$email=mysql_result($result,$i,"email");
$biography=mysql_result($result,$i,"biography");
$practice_areas=mysql_result($result,$i,"practice_areas");
echo "<b><a href=\"profile_edit.php?attorney_id=$attorney_id\">$firstname $lastname</a></b><br>
<b>E-Mail:</b> $email<br><b>Biography:</b> $biography<br><b>Practice Areas:</b> <br><br>";
$i++;
}
echo "Total No. of Attorneys = $num";
if(!$result)
{
echo "No data.";
}
mysql_free_result($result);
echo "<br>";
echo "<br>";
echo "<a href=\"profile_add.php\">Add Attorney Profile";
?>
My problem is it displays all the information except the checkboxes. Pls give me a soln. for this.
Thanx.