0

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

5
  • replace if(!in_array($engineer_id, $_POST['engineer'])){ with if(isset($_POST['engineer']) && is_array($_POST['engineer']) && !in_array($engineer_id, $_POST['engineer'])){ Commented May 2, 2013 at 20:54
  • @Ejay, thanks for that but it stops the page from working Commented May 2, 2013 at 21:02
  • stops the page from working you mean you get some error? or it doesn't execute the delete statement? Commented May 2, 2013 at 21:13
  • no error, it just doesn't delete the engineer from the job Commented May 2, 2013 at 21:16
  • you should post your complete generated HTML for the <form> that's making POST request to the script. Commented May 2, 2013 at 21:35

1 Answer 1

2

If only 1 engineer is checked, the value might be a string, not an array.

Then, I would add an is_array() before calling the in_array():

if(is_array($_POST['engineer']) && !in_array($engineer_id, $_POST['engineer'])){
    // code          
}

There are actually a lot of ways that you could structure that, but you should get the idea.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, If I add that it stops the page from working, the checked engineer does not get removed from the job
Before your foreach, try adding this: $engineer = is_array($_POST['engineer']) ? $_POST['engineer'] : array($_POST['engineer']); Then, in your original code, change the $_POST['engineer'] on that 4th line to the variable $engineer. Or else, check your logic and SQL results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.