2013-11-28 2 views
0

하나의 양식 제출의 결과 인 데이터 테이블이 있는데 양식의 유형은 DATETIME-LOCAL 인 start_date 및 end_date입니다. 내 데이터베이스에서날짜가있는 데이터 테이블 검색

는이 DATETIME,
내가 일하지 날짜와 시간을 기준으로 내 데이터 테이블에 검색 기능을 추가 할,

유형에 따라 검색

다른 유형과 (AM과 PM을 표시하지 않음) NAME이 (가) 작동하지만 날짜가 표시되지 않으며 오류 및 결과가 표시되지 않습니다.

<form action="search.php" id="searchform" method="POST" class="searchbox-container"> 
    <input type="text" id="searchbox" placeholder="Search" name="searchbox" class="searchbox" /> 
    <input type="date" name="date"> 

    <select name="select" id="select"> 
     <option value="type">Type</option> 
     <option value="name">Name</option> 
    </select> 
    <input type="submit" name="search" class="searchbox-btn" value="Go" /> 

</form> 

<?php 

    if(isset($_POST['search'])){ 
     $search=preg_replace('#[^a-z 0-9?!]#i','',$_POST['searchbox']); 


     $user="admin"; 
     $pass="neehahs"; 
     $host="localhost"; 
     $db_name="eventregisteration"; 

     $con=mysqli_connect($host, $user, $pass, $db_name); 
     if(mysqli_connect_errno($con)){ 
      echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 

     $date=$_POST['date']; 


     if($_POST['select']=="type"){ 
      $sqlcommand="SELECT * FROM eventform WHERE event_type LIKE '%$search%'";  


     } 
     elseif($_POST['select']=="name"){ 
      $sqlcommand="SELECT * FROM eventform WHERE event_name LIKE '%$search%'";   


     } 
     else { 
      if($date!=0) 
       $sqlcommand="SELECT * FROM eventform WHERE start_date='$date' ORDER BY start_date DESC";  


     } 
     $sqldata=mysqli_query($con,$sqlcommand) 
      or die("Error Getting Data"); 

     $count=mysqli_num_rows($sqldata); 
     if($count!=0){ 

      echo "<table border=2 bordercolor=#440000 cellpadding=2 bgcolor=#DDDDDD width=100%>"; 
      echo "<tr bgcolor=#555555 style=font-size:18px align=center><th>Event Code</th><th>Event Name</th><th>Event Type</th><th>Event Level</th><th>Start Date</th> 
     <th>End Date</th><th>Point</th><th>Picture</th><th>Video</th><th>Description</th></tr>"; 
      while($row=mysqli_fetch_array($sqldata)){ 
       echo "<tr align=center><td>"; 
       echo $row['event_code']; 

       echo "</td><td>"; 
       echo $row['event_name']; 
       echo "</td><td>"; 

       echo $row['event_type']; 
       echo "</td><td>"; 

       echo $row['event_level']; 
       echo "</td><td>"; 

       echo $row['start_date']; 
       echo "</td><td>"; 

       echo $row['end_date']; 
       echo "</td><td>"; 

       echo $row['points']; 
       echo "</td><td>"; 

       echo "<a href=http://localhost/greenstudio/".$row['pic'].">".$row['pic']."</a>"; 
       echo "</td><td>"; 

       echo "<a href=http://localhost/greenstudio/".$row['video'].">".$row['video']."</a>"; 
       echo "</td><td>"; 

       echo $row['description']; 
       echo "</td></tr>"; 

      } 
      echo "</table>"; 

     }else{ 
      echo "hi"; 
      $search_output="<hr/>0 Results for<strong>$search</strong><hr/>"; 

     } 
    } 

?> 

답변

1
$dateYYYY-MM-DD 형식으로 여기에 (예를 들어 2013-11-27)

있어야 할 곳에이

SELECT * 
    FROM eventform 
WHERE start_date >= '$date' 
    AND start_date < '$date' + INTERVAL 1 DAY 
ORDER BY start_date DESC 

처럼 보일 수 있습니다 인덱스 친화적 인 방법으로 기입 해

귀하의 질의가 SQLFiddle 데모

추가 참고 : 이미 mysqli을 사용하고 있으므로 쿼리 문자열을 보간하는 대신 prepared statements을 배우고 사용하는 것이 좋습니다. 이는 쿼리 문자열을 SQL 주입에 대해 열어 놓은 상태로 두는 것입니다.

관련 문제