Jason Gearhart
Jason Gearhart

Reputation: 47

A syntax error I cannot find

Ok. This code is returning a syntax error of Invalid argument supplied for foreach()

I have gone through this code a lot and cannot for the life of my find the error.

I am still learning and trying out things so please be gentle.

<?php
include ('c1.php');
if ($_COOKIE["auth"] == "1") {
    $display_block = "<p>You are an authorized user.</p>";
} else {
    header("Location: userlogin.html");
    exit;
}
doDB();
$display_block = "<h1>Results</h1>";
if (isset($_POST['search']) && !empty($_POST['search'])) {
    foreach ($_POST['search'] as $key => $value) {
        if ($value == 1)
            $search[] = "$key";
        $searchstring = implode(' AND ', $search);
        $post_map = array(
            'postcode' => 'candididate_contact_details.postcode'
        );
        if (isset($_POST['postcode']) && !empty($_POST['postcode'])) {
            foreach ($_POST['postcode'] as $key => $value) {
                if (array_key_exists($key, $post_map))
                    $search[] = $post_map[$key] . '=' . mysql_real_escape_string($value);
                echo $searchstring;
                $query = "SELECT candidate_id.master_id, candidate_contact_details.first_name, candidate_contact_details.last_name, candidate_contact_details.home_phone, candidate_contact_details.work_phone, candidate_contact_details.mobile_phone, candidate_contact_details.email FROM candidate_id, candidate_contact_details, qualifications, security_experience, previous_career WHERE qualifications.active = 'finished' and candidate_id.master_id = candidate_contact_details.master_id and candidate_id.master_id = qualifications.master_id and candidate_id.master_id = security_experience.master_id and candidate_id.master_id = previous_career.master_id and $searchstring";
                $query_res = mysqli_query($mysqli, $query)
                        or die(mysqli_error($mysqli));
                // $search = mysqli_query($mysqli, $query)or die(mysqli_error($mysqli));
                {
                    $display_block .= "
    <table width=\"98%\" cellspacing=\"2\" border=\"1\">
    <tr>
    <th>Registration Number</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Home Number</th>
    <th>Work Number</th>
    <th>Mobile Number</th>
    <th>E-Mail</th>
    </tr>";
                    while ($result = mysqli_fetch_array($query_res)) {
                        $regnum = $result['master_id'];
                        $first_name = $result['first_name'];
                        $last_name = $result['last_name'];
                        $home_phone = $result['home_phone'];
                        $work_phone = $result['work_phone'];
                        $mobile_phone = $result['mobile_phone'];
                        $email = $result['email'];
                        $display_block .= "
    <tr>
    <td align=\"center\">$regnum <br></td>
    <td align=\"center\">$first_name <br></td>
    <td align=\"center\">$last_name <br></td>
    <td align=\"center\">$home_phone <br></td>
    <td align=\"center\">$work_phone <br></td>
    <td align=\"center\">$mobile_phone <br></td>
    <td align=\"center\">$email <br></td>
    </tr>";
                    }
                    $display_block .= "</table>";
                }
            }
        }
    }
}
?>
<html>
    <head>
        <title> Display results</title>
    </head>
    <body>
        <?php echo $display_block; ?>
    </body>
</html>

Upvotes: 1

Views: 105

Answers (4)

roselan
roselan

Reputation: 3775

this looks suspect to me:

foreach ($_POST['postcode'] as $key=>$value) {

                if (array_key_exists($key, $post_map))

Upvotes: 0

ceejayoz
ceejayoz

Reputation: 180125

Ok. This code is returning a syntax error of Invalid argument supplied for foreach()

This is generally because you're passing something that isn't an array to foreach, which expects an array. Two solutions:

Wrap the call in an is_array call.

if(is_array($_POST['search']) {
    foreach($_POST['search'] as $key=>$value){

Cast the variable to an array first.

foreach((array)$_POST['search'] as $key=>$value){

You'll need to make $_POST['search'] actually contain an array for any of this to ever fire. See @enyo's answer for how to do that.

Upvotes: 0

enyo
enyo

Reputation: 16706

The cause of this error is probably the value you are submitting with POST.

You wrote:

if(isset($_POST['search']) && !empty($_POST['search'])){
    foreach($_POST['search'] as $key=>$value){
    etc...

So you are assuming the value is an array.

To actually send an array in the post var 'search', you have to define the input fields like this:

<input type="text" name="search[]" value="" />

(The brackets tell PHP that it is an array).

If you already know this, then you should simply check if the submitted $_POST['search'] is actually an array with is_array().

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96286

Your form doesn't send array values to those post fields. You better check with is_array.

If you are passing strings, use explode.

Upvotes: 1

Related Questions