PHP / SQLITE3问题

时间:2019-03-11 21:59:41

标签: php sqlite

尝试添加帖子时收到错误-它刚刚开始发生:

  

警告:SQLite3 :: exec():在“ t”附近:第9行出现语法错误

<?php 
    include '../views/addBlog.html';
    require '../model/db.php';
    $result = "";
    if(isset($_POST['title']) && isset($_POST['content'])) {
        $title = $_POST['title'];
        $content = $_POST['content'];

    if ($db->exec("INSERT INTO forum (title, content) VALUES('$title', 
         '$content')")) {
        $result = "Added";
    }
    else {
        $result = "not added";
    }
}

我也无法弄清楚如何接受参数并查询SQLITE3中的特定行。如果我对ID=1``, it pulls data, but not when I use $ id`

进行硬编码

Index.php:

<?php 
    require_once('../model/db.php');
    include('header.html');
    $statement = $db->query('SELECT * FROM forum ORDER BY post');
 ?>

<?php while($row = $statement->fetchArray(SQLITE3_ASSOC)) { 
        echo '<div class="blog">';
            echo '<h2><a href="viewBlog.php?id=1">'.$row['title'].'</a> 
               </h2>';
            echo '<p>Posted on: '.$row['post'];
            echo '<p>'.$row['content'].'</p>';
            echo '<p><a href="viewBlog.php?id='.$row['ID'].'">'.'Read 
                 more</a></p>';
        echo '</div>';
    }
?>

viewBlog.php

<?php 
    require_once('../model/db.php');
    include('header.html');
    $id = $_GET['id'];
    $statement = $db->query('SELECT * FROM forum WHERE ID=$id');
?>

<body>
    <h2>Recent Questions</h2>
        <?php while($row = $statement->fetchArray()) { 
         echo '<h2>'.$row['title'].'</a></h2>';
        echo '<p>Posted on: '.$row['post'];
         echo '<p>'.$row['content'].'</p>';
    } ?>  
     <p><a href="index.php">Go Back</a></p>
</body>

1 个答案:

答案 0 :(得分:0)

请改用准备功能http://php.net/manual/en/sqlite3.prepare.php

要正确显示行,请使用htmlentities http://php.net/manual/en/function.htmlentities.php

<?php 
        include '../views/addBlog.html';
        require '../model/db.php';
        $result = "";
        if(isset($_POST['title']) && isset($_POST['content'])) {
            $title = $_POST['title'];
            $content = $_POST['content'];

        $stmt = $db->prepare("INSERT INTO forum (title, content) VALUES(:title,:content)");
        $stmt->bindParam(':title', $title);
        $stmt->bindParam(':content', $content);
        $result = $stmt->execute();
        var_dump($result);
相关问题