How can i checked checkbox data form mysql database?

if student have multiple courses checkboxes is not checked…

<?php
$level3=$_GET['id'];

$re=mysql_fetch_array(mysql_query("select * from `students` where id='$level3'"));

$course_qry=mysql_query("select * from `course` where status='1'");
while($course_fetch=mysql_fetch_array($course_qry)){
?>
<input type="checkbox" name="course" <?php if($re['course']==$course_fetch['id']){ echo "checked='checked'"; } ?> />
<?php echo $course_fetch['course']; ?><br />
<?php } ?>

It’s not working because this bit of code

<?php if($re['course']==$course_fetch['id']){ 

will never return true when there is more than one value in the column.

Rearrange your database table so that instead of having comma-separated values within the “course” column, you have a separate table to store the courses that each student is taking. Check out “database normalisation” for more reading, trying to store more than one value in a single column is poor design.

You could use explode() on the course column and check whether the course id is present in the resulting array, but it’ll only get more complex as you need to do more with the data, so better to arrange it properly now.

2 Likes

either validate/sanitize the id before copying it or get rid of the unnecessary $level3 variable and use the id through the rest of the code to make it more obvious that there is a huge security hole there.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.