2014-06-20 111 views
1

我試圖在特定日期範圍內獲取表格時似乎遇到問題。這是我的PHP代碼。我嘗試使用BETWEEN和< =來獲取日期範圍,但它似乎不起作用。當我將結果打印到控制檯時,我只是得到一個Uncaught SyntaxError:意外的數字。有什麼建議麼?MySQL和PHP的日期範圍

if($_POST['subdate'] != "") 
    { 

     $sdate = dateClean($_POST['subdate']); 
     $edate = dateClean($_POST['subdateend']); 
     if($checkWhereVar==0) 
     { 

      if($_POST['subdateend'] != "") 
      { 

       $longQuery.= "SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate.""; 
       echo ("<script>console.log('$longQuery')</script>"); 
      } 
      else{ 

       $date = date('Y/m/d H:i:s'); 
       $edate = $date; 
       $longQuery.= "SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate.""; 

      } 

      //$longQuery.= " SubmissionDate BETWEEN = '".$sdate."'"; 
      $checkWhereVar = 1; 
     } 
     else 
     { 
      $date = date('Y/m/d H:i:s'); 
      $edate = $date; 
      echo ("<script>console.log('in sub date else')</script>"); 
      $longQuery.= "SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate.""; 
     } 
     $filter .= " Submission Date: <i>".$_POST['subdate']."</i>"; 
    } 
+0

**警告**你不應該滾動自己的SQL注入保護! –

+1

在執行併發布之前立即回顯查詢。 –

+0

「Uncaught SyntaxError」其中和全部細節。 – JakeGould

回答

0

這是很難沒有看到腳本的其餘部分來診斷,但似乎你的字符串連接可能會在這樣行的一個因素:

$longQuery.= "SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate.""; 

在只需添加一個空格像這樣的開始:

$longQuery.= " SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate.""; 

此外,你似乎在字符串的末尾缺少單引號。因此,只要做到這一點:

$longQuery.= " SubmissionDate >= '$sdate' AND SubmissionDate <= '$edate'"; 

當您使用雙引號,你可以做字符串替換,所以你不必來連接這些行以.,每一個字符串顯示出來。

也就是說,這裏是我清理你的代碼與上面提到的東西的每個實例解決。

if($_POST['subdate'] != "") 
    { 

     $sdate = dateClean($_POST['subdate']); 
     $edate = dateClean($_POST['subdateend']); 
     if($checkWhereVar==0) 
     { 

      if($_POST['subdateend'] != "") 
      { 

       $longQuery.= " SubmissionDate >= '$sdate' AND SubmissionDate <= '$edate'"; 
       echo ("<script>console.log('$longQuery')</script>"); 
      } 
      else{ 

       $date = date('Y/m/d H:i:s'); 
       $edate = $date; 
       $longQuery.= " SubmissionDate >= '$sdate' AND SubmissionDate <= '$edate'"; 

      } 

      //$longQuery.= " SubmissionDate BETWEEN = '$sdate'"; 
      $checkWhereVar = 1; 
     } 
     else 
     { 
      $date = date('Y/m/d H:i:s'); 
      $edate = $date; 
      echo ("<script>console.log('in sub date else')</script>"); 
      $longQuery.= " SubmissionDate >= '$sdate' AND SubmissionDate <= '$edate'"; 
     } 
     $filter .= " Submission Date: <i>".$_POST['subdate']."</i>"; 
    } 
+0

感謝這工作 – user3761203