I have a form that displays a checkbox for each engineer in a table. When the form is submitted if the engineer's checkbox isn't ticked they are removed from the job ($diary_id).
The code as it is at the moment works but a page briefly displays this error before going back to the next page.
in_array() expects parameter 2 to be array, null given error
The code on the form...
$sql = "SELECT * FROM users WHERE type = 'engineer' ORDER BY first_name";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){ ?>
<div style="width:70px; float:left">
<input type="hidden" name="engineer_on_page[]" value="<? echo $row['id']; ?>"/>
<input type="checkbox" name="engineer[]" <?
$eng_id= $row['id'];
$result2 = mysql_query("SELECT * FROM calls_engineers WHERE call_ce = '$diary_id' AND engineer_ce = '$eng_id'");
if((mysql_num_rows($result2)) > 0)
{ echo 'checked="checked"';
?> value="<? echo $row['id']; ?>" />
<? echo ' '.$row['first_name']; }
else {
echo "value=".$row['id']." />";
echo ' '.$row['first_name'];
}
?>
</div>
<? } ?>
The mysql to remove the unselected engineers
foreach($_POST['engineer_on_page'] as $engineer_id) {
$sql = "SELECT * FROM calls_engineers WHERE call_ce = '$diary_id' AND engineer_ce = '$engineer_id'";
$result = mysql_query($sql)or die(mysql_error());
if(!in_array($engineer_id, $_POST['engineer'])){
if(mysql_num_rows($result) > 0){
$sql = "DELETE FROM calls_engineers WHERE engineer_ce = '$engineer_id' AND call_ce = '$diary_id'";
$result = mysql_query($sql)or die(mysql_error());
}
}
}
I have tried to initialise $_POST['engineer'] as an array ($_POST['engineer'] = array();) before the foreach loop which stops the error, but it also stops the page working, no error messages, it just doesn't work any more.
I am aware that the page needs to be updated to mysqli but I would like to resolve this issue before I do.
Thanks
if(!in_array($engineer_id, $_POST['engineer'])){withif(isset($_POST['engineer']) && is_array($_POST['engineer']) && !in_array($engineer_id, $_POST['engineer'])){stops the page from workingyou mean you get some error? or it doesn't execute the delete statement?<form>that's making POST request to the script.