使用包含日期的php变量搜索mysql date列

时间:2019-05-13 12:45:44

标签: php mysql date

在HTML页面上,我显示来自mysql查询的记录,包括日期列。 然后,我使用数据填充一个下拉列表,以便用户可以选择与另一个数据集关联的日期以显示不同的信息。 这段代码可以正常运行,并且在POST之后,可以使用php变量在另一个文件中访问所选的日期。

然后,我尝试在新的MYSQL查询中使用该PHP变量,以根据所包含的日期检索更多记录。该查询未检索到任何记录,我也看不出原因。如果我对日期进行硬编码,例如“ 2019-03-09”,而不是使用变量,则它可以工作并检索记录。

所以我的问题与日期格式有关系吗?

代码示例(html文件)

<?php 
echo "<p class='tabcentre'><b>User is.......: </h3>".$User."</b><br>";
$sql = "SELECT distinct SessionDescription, DateCreated FROM SessionPlanTable where UserLevel=$UL";
if($result = mysqli_query($conn, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table class='tabcentre'>";
        echo "<tr>";
            echo "<th>Session Description</th>";
            echo "<th>Date Created</th>";
        echo "</tr>";
     while($row = mysqli_fetch_array($result)){
        echo "<tr>";
        echo "<td>" . $row['SessionDescription'] . "</td>";
        echo "<td>" . $row['DateCreated'] . "</td>";    
        echo "</tr>";
    }
    echo "</table>";
    } else{
    echo "No records matching your query were found.";
    }
    } else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($sql);
}

$result = mysqli_query($conn, $sql);
$select= '<form action="SecureReadSessionPlanTable.php" method="post"><br><p style="color:blue;">Please select your chosen Session Plan to view from the list below:</p><select name="Selected"><optgroup>';
while($rs=mysqli_fetch_array($result))
{
  $select.='<option value="'.$rs['DateCreated'].'">'.$rs['DateCreated'].'</option>';
}
$select.='</optgroup></select><br><br><input type="visible" id="SP" name="SP" value="1"><input type="submit" value="Submit"></form><br><br>';
echo $select;
?>

PHP文件代码示例

    $userid = $_SESSION['id'];
    $DateCreated=$_POST['Selected'];

    $sqltest ="SELECT SessionPlanTable.ExerciseID, SessionPlanTable.userid, ExerciseTable.ExerciseID, ExerciseTable.ExerciseCategoryID, ExerciseTable.ExerciseAspect, ExerciseTable.PlayerLevelID, ExerciseTable.CoachingPoints1, ExerciseTable.CoachingPoints2, ExerciseTable.CoachingPoints3, ExerciseTable.CoachingPoints4, ExerciseTable.CoachingPoints5, ExerciseTable.Instruction1, ExerciseTable.Instruction2, ExerciseTable.Instruction3, ExerciseTable.Instruction4, ExerciseTable.Instruction5, ExerciseTable.PathwayData, ExerciseTable.VideoPathway, ExerciseTable.ExerciseStyle ,ExercisePathwayTable.PathwayID, ExercisePathwayTable.PathwaySequence, ExercisePathwayTable.tablelocator, PathwayTable.StartPathwayID, PathwayTable.EndPathwayID, PathwayTable.StartPathwayXCoord, PathwayTable.StartPathwayYCoord, PathwayTable.EndPathwayXCoord, PathwayTable.EndPathwayYCoord
    FROM ExerciseTable
    JOIN ExercisePathwayTable
    ON ExerciseTable.ExerciseID=ExercisePathwayTable.ExerciseID
    JOIN PathwayTable
    ON ExercisePathwayTable.PathwayID=PathwayTable.PathwayID
    JOIN SessionPlanTable
    ON ExerciseTable.ExerciseID=SessionPlanTable.ExerciseID
    WHERE SessionPlanTable.userid=$userid and SessionPlanTable.DateCreated=$DateCreated
    ORDER BY ExerciseTable.ExerciseID ASC, ExercisePathwayTable.PathwaySequence DESC;"; 
    $result = mysqli_query($conn,$sqltest);
    if(mysqli_num_rows($result)==0) echo("exercises were NOT found for the selected date"); else echo("exercises were found for this date");

非常感谢收到任何帮助

1 个答案:

答案 0 :(得分:0)

FWIW,我发现这更容易阅读,并注意到'$ DateCreated'用反引号引起来-尽管最好使用正确准备和绑定的语句。

SELECT s.ExerciseID
     , s.userid
     , e.ExerciseID
     , e.ExerciseCategoryID
     , e.ExerciseAspect
     , e.PlayerLevelID
     , e.CoachingPoints1
     , e.CoachingPoints2
     , e.CoachingPoints3
     , e.CoachingPoints4
     , e.CoachingPoints5
     , e.Instruction1
     , e.Instruction2
     , e.Instruction3
     , e.Instruction4
     , e.Instruction5
     , e.PathwayData
     , e.VideoPathway
     , e.ExerciseStyle 
     , ep.PathwayID
     , ep.PathwaySequence
     , ep.tablelocator
     , p.StartPathwayID
     , p.EndPathwayID
     , p.StartPathwayXCoord
     , p.StartPathwayYCoord
     , p.EndPathwayXCoord
     , p.EndPathwayYCoord
  FROM ExerciseTable e
  JOIN ExercisePathwayTable ep
    ON ep.ExerciseID = e.ExerciseID
  JOIN PathwayTable p
    ON p.PathwayID = ep.PathwayID 
  JOIN SessionPlanTable s
    ON s.ExerciseID = e.ExerciseID
 WHERE s.userid = $userid 
   AND s.DateCreated = '$DateCreated'
 ORDER 
    BY e.ExerciseID ASC
     , ep.PathwaySequence DESC;