I have a form in my website that I want to update the information of the user (Edit Profile) and some of the informations of the user has to do with the diseases that might a user has. I have checkboxes in order to select any disease but how I can have already checked checkboxes according to the data from my database table. The code for the checkboxes is the below...
<label id="Disease">Your Disease(s):</label><br />
<!--emfanizei tis epiloges gia ta diseases me basi auta p exoume sti basi mas -->
<?php
$sql = "SELECT name FROM disease";
$query_resource = mysql_query($sql);
while( $name = mysql_fetch_assoc($query_resource) ):
?>
<span><?php echo $name['name']; ?></span>
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>" /><br />
<?php endwhile; ?>
My database table is named as patient and look likes this
And my checkboxes look like this...
My update code ...
if(isset($_POST['username'])){
$fname = mysql_real_escape_string($_POST["fname"]);
$lname = mysql_real_escape_string($_POST["lname"]);
$email = mysql_real_escape_string($_POST["email"]);
$password = mysql_real_escape_string($_POST["password"]);
$age = ($_POST["age"]);
$disease = implode(",",$_POST["disease"]);
$query="UPDATE patient
SET fname = '$fname', lname = '$lname', email = '$email', password = '$password', age='$age', disease='$disease'
WHERE username='$username'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=0){
echo "<p>($username) Record Updated<p>";
}else{
echo "<p>($username) Not Updated<p>";
}
}
Aucun commentaire:
Enregistrer un commentaire