2015-09-12 91 views
0

只要選擇了兩個字段,下面的工作就會生效。如果兩個都沒有選擇它的作品,但我的問題是,當只有一個的字段被選中,它不起作用。它引發未綁定的參數問題。PHP PDO準備查詢問題

我試過的0虛假的值設置爲兩個變量,但這不會工作,因爲隨後的查詢是從那裏= 0

想法選擇?

public static function searchProfile($status, $fundamt) 
    { 
     $database = DatabaseFactory::getFactory()->getConnection(); 
     $sql = "SELECT profile_id, profile_name, profile_url, finance_fundingtype, finance_equitypercent, finance_loanrate, finance_loanlength, finance_fundingamount, info_tradingstatus, info_elevatorpitch, info_patentable, info_industry, info_industry1, info_industry2, info_industry3, info_industry4, seeker_logo_url FROM profile_seeker WHERE profile_status = '1' "; 

     if ($status) { 
     $sql .= "AND info_tradingstatus IN (:status) "; 
     } 
     if ($fundamt) { 
      $sql .= "AND finance_fundingamount <= :fundamt "; 
     } 

     $query = $database->prepare($sql); 
     $query->execute(array(':status' => $status, ':fundamt' => $fundamt)); 
     $profiles = array(); 
$profiles[$profile->profile_id] = new stdClass(); 
      $profiles[$profile->profile_id]->profile_id = $profile->profile_id; 
      $profiles[$profile->profile_id]->profile_name = $profile->profile_name; 
      $profiles[$profile->profile_id]->profile_url = $profile->profile_url; 
      $profiles[$profile->profile_id]->finance_fundingtype = $profile->finance_fundingtype; 
      $profiles[$profile->profile_id]->finance_equitypercent = $profile->finance_equitypercent; 
      $profiles[$profile->profile_id]->finance_loanrate = $profile->finance_loanrate; 
      $profiles[$profile->profile_id]->finance_loanlength = $profile->finance_loanlength; 
      $profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount; 
      $profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus; 
      $profiles[$profile->profile_id]->info_elevatorpitch = $profile->info_elevatorpitch; 
      $profiles[$profile->profile_id]->info_patentable = $profile->info_patentable; 
      $profiles[$profile->profile_id]->info_industry = $profile->info_industry; 
      $profiles[$profile->profile_id]->info_industry1 = $profile->info_industry1; 
      $profiles[$profile->profile_id]->info_industry2 = $profile->info_industry2; 
      $profiles[$profile->profile_id]->info_industry3 = $profile->info_industry3; 
      $profiles[$profile->profile_id]->info_industry4 = $profile->info_industry4; 
      $profiles[$profile->profile_id]->seeker_logo_url = $profile->seeker_logo_url; 
     } 
     return $profiles; 

回答

0

你可以嘗試檢查變量的設置和他們持有的值不爲0,這有一個字符串的長度比0長:

if (isset($status) && $status !== 0 && strlen($status) > 0) { 
    $sql .= "AND info_tradingstatus IN (:status) "; 
} 
if (isset($fundamt) && $fundamt!== 0 && strlen($fundamt) > 0) { 
    $sql .= "AND finance_fundingamount <= :fundamt "; 
} 

你也可以嘗試綁定手動參數:

$query = $database->prepare($sql); 
if (isset($status) && $status !== 0 && strlen($status) > 0) { 
    $query ->bindParam(':status',$status); 
} 
if (isset($fundamt) && $status !== 0 && strlen($fundamt) > 0) { 
    $query ->bindParam(':fundamt',$fundamt); 
} 
$query->execute(); 
0

你綁定錯誤來,因爲你忘了使用相同的if exists聲明您的綁定。

改變了...

if ($status) { 
    $sql .= "AND info_tradingstatus IN (:status) "; 
    } 
    if ($fundamt) { 
     $sql .= "AND finance_fundingamount <= :fundamt "; 
    } 

 $profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount; 
     $profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus; 

要這個......

if (isset($status) && $status != '') { 
    $sql .= "AND info_tradingstatus IN (:status) "; 
    } 
    if (isste($fundamt) && $fundamt != '') { 
     $sql .= "AND finance_fundingamount <= :fundamt "; 
    } 

 if (isset($status) && $status != '') { 
     $profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount; 
     } 
     if (isste($fundamt) && $fundamt != '') { 
     $profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus; 
     } 

如果一個或兩個文本框都爲空,這將停止綁定。

快樂編碼!