dongqun5769 2018-04-17 16:31
浏览 154
已采纳

使用PHP和HTML输入搜索Sqlite数据库

EDIT - CODE BELOW SHOWS CORRECTIONS

I need to create a search function that will search a SQLite database for a certain date range.

For example:

  • if the user were to input "7", it would look for all records that have a date within the next 7 days.
  • If the user were to input "-7" it would look for all records in the last 7 days.

So far I have:

if (!empty($_POST)) {
    $sql = "SELECT * from table where record_date between date('now') and date('now', :record_date||' days')"; 
    $stmt = $db->prepare($sql);
    $stmt->bindValue(':record_date' , '%'.$webdata['record_date'].'%', SQLITE3_TEXT);
    $result = $stmt->execute();
    while ($row = $result->fetchArray()) { 
        echo '<p>' . htmlspecialchars($row['id'])  . " : " . 
        htmlspecialchars($row['record_date']).'</p>';
    }
}
?>

This doesn't seem to bring through any results.

If I use SqliteOnline.com to query the database this does work fine with the query:

SELECT * from table where record_date between date('now') and date('now', '7 days')
  • 写回答

2条回答 默认 最新

  • dousuohe5882 2018-04-18 13:16
    关注

    Named placeholders shouldn't be quoted as they are then interpreted as a literal value.

    Try changing:

    date('now', ':record_date days')
    

    to:

    date('now', :record_date||' days')
    

    You also need to modify the following from:

    $stmt->bindValue(':record_date' , '%'.$webdata['record_date'].'%', SQLITE3_TEXT);
    

    to:

    $stmt->bindValue(':record_date' , $webdata['record_date'], SQLITE3_TEXT);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算