2011-09-18 122 views
1

好的。此代码返回为foreach提供的无效参数的语法错误()语法错误我找不到

我已经经历了这个代码很多,不能为我的生活发现错误。

我还在学习和尝试的东西,所以请温柔。

<?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> 
+0

哪一行*是语法错误? –

+0

对不起忘了。 foreach($ _POST ['postcode'] as $ key => $ value){ –

+0

$ _POST ['postcode']上var_dump的输出是什么?它是一个数组吗? –

回答

1

此错误的原因可能是你用POST提交的值。

您写道:

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

那么,你是假设的值是一个数组。

在岗VAR“搜索”实际发送的数组,你必须这样定义输入字段:

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

(括号告诉PHP,这是一个数组)。

如果您已经知道这一点,那么您应该简单地检查提交的$ _POST ['search']是否实际上是一个含有is_array()的数组。

1

您的表单不会将数组值发送到这些发布字段。你最好用is_array查询。

如果您传递字符串,请使用explode

0

这看起来犯罪嫌疑人对我说:

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

       if (array_key_exists($key, $post_map)) 
+0

这是它报告错误的地方。我只是看不到它 –

0

好的。此代码返回()的foreach为无效的论点提供的语法错误

这通常是因为你传递的东西是不是一个数组foreach,其预计的数组。两种解决方案:

将呼叫包装在is_array呼叫中。

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

首先将变量转换为数组。

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

你需要做$_POST['search']实际上包含了任何一个这样的阵列永远火。有关如何做到这一点,请参阅@ enyo的答案。