sqlite数据库

时间:2018-03-04 12:42:16

标签: php sqlite

您好我已成功编写一个查询,显示我的sqlite数据库中两个特定日期之间的记录。但是我不确定如何运行查询,用户将两个日期输入到字段中,并显示这两个日期之间的记录。这是我到目前为止编写的代码。第一批代码是在浏览器中完美运行的查询。第二批两批代码是我需要的用户放置类型查询的练习模板,但我不确定如何做到这一点。我想提供一些代码来确保我至少尝试过一些东西。非常感谢帮助

    <?php
// knew most part how yo connect to database and query. Got help with table display here "https://stackoverflow.com/questions/49025289/displaying-query-in-browser-with-table/49025603#49025603"
$db = new PDO('sqlite:daypilot.sqlite'); 
$start = '2018-02-20'; 
$end = '2018-02-25'; 
$sql = 'SELECT * FROM events WHERE end > ? AND start < ?'; 
$stmt = $db->prepare($sql); $stmt->execute([$start, $end]); 
$events = $stmt->fetchAll(PDO::FETCH_ASSOC); 

$table = '<table>';
foreach($events as $event) {
    $table .= '<tr>';
    $table .= '<td>' . $event['id'] . '</td>';
    $table .= '<td>' . $event['name'] . '</td>';
    $table .= '<td>' . $event['start'] . '</td>';
    $table .= '<td>' . $event['end'] . '</td>';

    $table .= '</tr>';
}
$table .= '</table>';
echo $table;
?>

<body>

<form action='http://localhost/home.php'>
    Title: <input type=text name=title /><br />
    Body: <input type=text name=body /> 
    <input type=submit />
</form>

</body>

</html>


<html>

<head></head>

<body>
<?php
$post = array(
    'title' => $GET['title'], 
    'body' => $GET['body']
);
echo "<h1>".$post['title']."</h1>";
echo "<p>".$post['body']."</p>";
echo "<hr />";
?>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

我认为你的问题只是查询逻辑。

您的WHERE子句

WHERE end > ? AND start < ?'

会生成

WHERE end > '2018-02-20' AND start < '2018-02-25'

而是这样做

$start = '2018-02-20'; 
$end = '2018-02-25'; 
$sql = 'SELECT * FROM events WHERE start > ? AND end < ?'; 
$stmt = $db->prepare($sql); 
$stmt->execute([$start, $end]); 

哪个应该生成

WHERE start > '2018-02-20' AND end < '2018-02-25'

并且有一个包裹着它的表格

<body>

<?php
    // make sure the form has actually been submitted
    // and its not just been loaded from a click on a link etc
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if ( isset($_POST['start'], $_POST['end']) ) {

            $db = new PDO('sqlite:daypilot.sqlite');

            $sql = "SELECT * FROM events WHERE start > ? AND end < ?"; 
            $stmt = $db->prepare($sql); 
            $stmt->execute([$_POST['start'], $_POST['end']]); 
            $events = $stmt->fetchAll(PDO::FETCH_ASSOC); 
            $table = '<table>';
            foreach ($events as $event) {
                $table .= '<tr>';
                $table .= '<td>' . $event['id'] . '</td>';
                $table .= '<td>' . $event['name'] . '</td>';
                $table .= '<td>' . $event['start'] . '</td>';
                $table .= '<td>' . $event['end'] . '</td>';
                $table .= '</tr>';
            }
            $table .= '</table>';
            echo $table;
        }
    }
?>
    <form action='http://localhost/home.php' method="POST">
        Title: <input type="text" name="start" /><br />
        Body: <input type="text" name="end" /> 
        <input type="submit" name="submit" />
    </form>
</body>
  

注意:如果您以'2018-02-20'之外的任何其他格式输入日期,则可能需要添加一些检查并将任何其他格式转换为正确的格式