2014-02-11 4 views
1

나는 PHP 페이지를 만들고 데이터 포스트가 다음을 위해이 쿼리를 실행하는 경우에는 isset 함수를 사용하지만 항상 3 개의 쿼리를 실행하지만 나 한 번에 하나의 쿼리 만 실행하고 싶다. 코드 : 여기한 번에 하나의 쿼리를 실행하는 방법은 무엇입니까?

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 매우 이례적인 일이다 사용 및 사용 - 상당히 그것을 깨는 생각 SQL에서 직접 $ _POST 변수 = 당신은 해킹 당할 것입니다. –

답변

1

은 배열의 두 fromdatetodate 인덱스가있다. 그 이유는 3 코드 블록이 실행됩니다. 당신은 단지 색인을 검사하고 있습니다, 당신은 또한 가치를 확인해야합니다. 즉, null이 아닙니다. 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 
} 
관련 문제