2014-02-11 120 views
1

我做了一個PHP頁面,我想如果數據發佈然後查詢運行這個我使用isset函數,但沒有爲我工作它總是運行三個查詢,但我想一次運行一個查詢我是如何做到這一點?這是我的代碼:如何一次運行一個查詢?

if(isset($_POST['fromdate'])){ 
       $fromdate=date('Y-m-d h:i:s',strtotime($_POST['fromdate'])); 
     echo $select="select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' and b.created_on>='".$fromdate."' group by r.buyer_id"; 
$res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__); 
$record=mysqli_num_rows($res); 
    $recepients = $recepients + $record;      
} 
if(isset($_POST['todate'])){ 
$todate=date('Y-m-d h:i:s',strtotime($_POST['todate'])); 
echo $select1="select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' and b.created_on>='".$todate."' group by r.buyer_id"; 
$res1 = $GLOBALS ['mysqli']->query ($select1) or die ($GLOBALS ['mysqli']->error . __LINE__); 
       $record1=mysqli_num_rows($res1); 
       $recepients = $recepients + $record1; 
      } 
      if(isset($_POST['fromdate']) && isset($_POST['todate'])){ 
      $fromdate=date('Y-m-d h:i:s',strtotime($_POST['fromdate'])); 
      $todate=date('Y-m-d h:i:s',strtotime($_POST['todate'])); 
       echo $select2="select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' and b.created_on BETWEEN '".$fromdate."' AND '".$todate."' group by r.buyer_id"; 
$res2 = $GLOBALS ['mysqli']->query ($select2) or die ($GLOBALS ['mysqli']->error . __LINE__); 
       $record2=mysqli_num_rows($res2); 
       $recepients = $recepients + $record2; 
      } 

,這裏是我的數組:

Array ([events] => Array ([0] => 7) [fromdate] => [todate] => February 11, 2014 2:55 PM [description] => [subject] => [fromname] => [replyto] => [senddatetime] => [timezone] => America/Los_Angeles [message] => [submit_skip] => Continue) 

,當我顯示的查詢然後給壞的結果,這裏是我的查詢

select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='7' and b.created_on>='1970-01-01 12:00:00' group by r.buyer_id 
select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='7' and b.created_on>='2014-02-11 02:55:00' group by r.buyer_id 
select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='7' and b.created_on BETWEEN '1970-01-01 12:00:00' AND '2014-02-11 02:55:00' group by r.buyer_id 
+0

使用,這是在如果ELSEIF,否則結構 – Jain

+0

另外,你的代碼是相當密集的 - 想想打破它顯著 - 也,使用$ GLOBALS是非常不尋常的,並使用$ _POST變量直接在SQL中=你會被黑客入侵。 –

回答

1

在你的數組中有fromdatetodate索引。這就是爲什麼3個代碼塊被執行。你只是檢查索引,你還需要檢查值。即,不爲空。也可以使用if..else。在這裏,您可以使用下面,

//if both fromdate and todate are not null 
if(isset($_POST['fromdate']) and $_POST['fromdate']!='' and isset($_POST['todate']) and $_POST['todate']!=''){ 
    //code 
} 
//if fromdate is not null 
else if(isset($_POST['fromdate']) and $_POST['fromdate']!=''){ 
    //code 
} 
//if todate is not null 
else if(isset($_POST['todate']) and $_POST['todate']!=''){ 
    //code 
} 
0

幽祕例如,在第一個例子中使用ght use!isset($ _ POST ['todate'])。

if(isset($_POST['fromdate']) && !isset($_POST['todate']){ 
    // code 
} 
if(isset($_POST['todate']) && !isset($_POST['fromdate']){ 
    // code 
} 
if(isset($_POST['fromdate']) && isset($_POST['todate'])){ 
    // code 
}