I have this code and it does not insert the values in the database. What am I doing wrong?
while ($rij = mysql_fetch_assoc($doquery2))
{
$user_id = $rij['id'];
}
$cat = $_POST['cat'];
foreach($cat as $key => $value)
{
//$sql = "";
if ($value > 0)
{
$sql = sprintf("INSERT INTO cat VALUES('','$user_id','%s');",$value);
mysql_query($sql);
}
}
In my form each checkbox is built as follow:
<input type="checkbox" name="cat[]" value="Lifestyle">
What am i doing wrong?
If I remember correctly, an unchecked value won’t get passed through to $_POST at all. So you can just strip the lines checking $value. I think you might be running into issues there with different browsers sending different values for a checked box. So try this: