I have a php code where data from my database are fetched in an array with checkbox corresponding each data . I already have the codes enabling updates to my table (for each data checked):
<?php
include("connect.php");
$user=$_SESSION['username'];
$updated = FALSE;
$submit=FALSE;
if(count($_POST) > 0){
$library = $_POST['library'];
array_map('intval',$library);
$library = implode(',',$library);
mysql_query("UPDATE ES_Students SET library=0") or trigger_error(mysql_error
(),E_USER_ERROR);
mysql_query("UPDATE ES_Students SET library=1 WHERE StudentNO IN ($library)") or trigger_error
(mysql_error(),E_USER_ERROR);
$submit=TRUE;
$updated=TRUE;
}
?>
My problem is, whenever no checkbox is checked, an error will occur that the library index is undefined and all. I know it’s got something to do with the $count($_POST)>0 but I don’t actually know how to fix this. I tried putting an else-clause to redirect it to another page but it will result to same error. Can anyone tell me what to do??
Use an
issetaround your post. PHP linkIf any data is sent at all, even a blank form,
isset($_POST)will return true, but with acount($_POST)of 0. So, you could also test for the exact items of the array you’re looking for to be sure it is infact in the$_POST: