2012-09-12 2 views
0

아래는 MYSQL 데이터베이스에서 날짜 범위를 검색하는 코드입니다. 날짜를 검색 할 수 있는데 테이블 표제 (Product Desc, New 또는 Own and Date)가 표시되지만 데이터베이스의 결과가 표시되지 않습니까?PHP가 MYSQL 타임 스탬프에서 결과를 표시하지 않음 검색

선택한 날짜 범위의 결과를 표시하기 위해 내 코드를 수정해야하는 방법에 대한 아이디어가 있습니까?

<?php 
if(!isset($_POST['find'])) 
{ 
?> 
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>"> 
<table width = "450" align = "center"> 
    <tr> 
    <td><b><i>Please enter date in the field below (i.e. 11-09-2012)</i></b></td> 
</tr> 
<tr> 
    <td> 
    From&nbsp;:&nbsp; 
    <input type = "text" name = "small"> 
    To&nbsp;:&nbsp; 
    <input type = "text" name = "large"></td> 
</tr> 
<tr> 
    <td align = "center"> 
     <input type = "submit" name = "find" value = "SEARCH"> 
     <input type = "reset" value = "CLEAR FORM"> 
     </td> 
    </tr> 
</table> 
</form> 
<?php 
} 
else 
{ 
$small = trim($_POST['small']); 
$large = trim($_POST['large']); 

$connection = mysql_pconnect("localhost", "user_name", "password") or die("Connection failed. ".myslq_error()); 
mysql_select_db("stock") or die("Unable to select db. ".mysql_error()); 
//Add 1 to the upper range, $large, else it won't make it inclusive 
$query = "SELECT * FROM main_stock WHERE curr_timestamp BETWEEN DATE_FORMAT(curr_timestamp,'%".$small."') AND DATE_FORMAT(curr_timestamp, '%".($large+1)."') ORDER BY curr_timestamp"; 
$result = mysql_query($query) or die(mysql_error()); 

echo "<table width = '500' align = 'center'>"; 
echo "<tr><b>"; 
    echo "<td>Product Description</td>"; 
    echo "<td>New or Own?</td>"; 
    echo "<td>Date</td>"; 
echo "</b></tr>"; 
while($record = mysql_fetch_object($result)) 
{ 
    echo "<tr>"; 
     echo "<td>".$record->product_desc."</td>"; 
     echo "<td>".$record->newown."</td>"; 
     $year_part_of_date = explode('-', $record->curr_timestamp); 
    echo "<td>".$year_part_of_date[0]."</td>"; 
     //if you want the full date replace the $year_part_of_date[0] with $record->date 
    echo "</tr>"; 
} 
echo "</table>"; 

} 

?> 
+0

이 쿼리는 작품을 생성하고 있음을 확인 했, 당신은 다시 결과를 얻고 있다고? – andrewsi

+2

데이터베이스에서 직접 쿼리를 실행하면 어떻게됩니까? $ small과 $ large 값을 사용하여 날짜 문자열의 형식을 지정하는 것은 말이되지 않습니다. –

+1

사용자가 감염된 변수를 이스케이프 처리하지 않았으므로이 스크립트에 SQL 삽입 취약점이있을 수 있습니다. – halfer

답변

0

귀하의 질문이 유효하지 않다고 생각합니다.

curr_timestampYYYY-MM-DD HH:MM:SS 형식의 MySQL datetime 필드라고 가정하면 쉽게 비교할 수 있도록이 형식으로 입력을 변경해야합니다. 도움이 될 $small$large 형식에 대해 자세히 설명 할 수 있다면.

당신은 아마 당신의 쿼리가이 형식 $small_formatted$large_formatted가 제대로 date('Y-m-d H:i:s', $input_timestamp)

0
  1. 스위치 로직의 순서 같은 것을 사용 시간을 포맷

    SELECT * FROM main_stock 
    WHERE curr_timestamp BETWEEN '$small_formatted' AND '$large_formatted' 
    ORDER BY curr_timestamp ASC 
    

    에 있어야합니다. 귀하의 프로세스가 else이되기를 원하지 않습니다.

  2. 절차 및 객체 스타일을 결합하는 것처럼 보입니다. 하나를 선택.
  3. 모두 선택하지 마십시오 (SELECT *). 항상 열 목록을 지정하십시오.
  4. 날짜의 형식이 잘못되었습니다. 데이터베이스 형식 (YYYY-MM-DD)과 일치하도록 입력 날짜를 형식화해야합니다. 나는 curr_timestampYYYY-MM-DD이라고 가정하고 있습니다.
  5. deprecated이므로 mysql_ 기능 사용을 중지해야합니다.
  6. SQL 삽입을 방지하기 위해 prepared statements를 사용하십시오.

나는이 테스트를하지 않은,하지만 이것은 내가 위에 열거 한 문제가 해결

<?php 
if(isset($_POST['find'])) 
{ 
    $link = mysqli_connect("localhost", "user_name", "password", "stock"); 

    if (mysqli_connect_error()) { 
    die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); 
    } 

    $stmt = mysqli_prepare($link, 'SELECT * FROM main_stock WHERE curr_timestamp ' . 
    "BETWEEN DATE_FORMAT(?, '%m-%d-%Y') " . 
    "AND DATE_FORMAT(?, '%m-%d-%Y') ORDER BY curr_timestamp"); 

    mysqli_bind_param($stmt, 'ss', $_POST['small'], $_POST['large']) or die(mysqli_error($dbh)); 

    $result = mysqli_stmt_execute($stmt) or die(mysqli_error($link)); 

    echo "<table width = '500' align = 'center'>"; 
    echo "<tr><b>"; 
    echo "<td>Product Description</td>"; 
    echo "<td>New or Own?</td>"; 
    echo "<td>Date</td>"; 
    echo "</b></tr>"; 

    while($record = mysqli_fetch_assoc($result)) { 
    echo "<tr>"; 
    echo "<td>" . $record[product_desc] . "</td>"; 
    echo "<td>" . $record[newown] . "</td>"; 

     $year_part_of_date = explode('-', $record[curr_timestamp]); 

    echo "<td>".$year_part_of_date[0]."</td>"; 

     //if you want the full date replace the $year_part_of_date[0] with $record->date 
    echo "</tr>"; 
    } 

    echo "</table>"; 

    mysqli_close($link); 
} 
else 
{ 
?> 
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>"> 
<table width = "450" align = "center"> 
    <tr> 
    <td><b><i>Please enter date in the field below (i.e. 11-09-2012)</i></b></td> 
</tr> 
<tr> 
    <td> 
    From&nbsp;:&nbsp; 
    <input type = "text" name = "small"> 
    To&nbsp;:&nbsp; 
    <input type = "text" name = "large"></td> 
</tr> 
<tr> 
    <td align = "center"> 
     <input type = "submit" name = "find" value = "SEARCH"> 
     <input type = "reset" value = "CLEAR FORM"> 
     </td> 
    </tr> 
</table> 
</form> 
<?php 
} 

?> 
관련 문제