2011-09-18 126 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的答案。