【问题标题】:Update blog post with PHP OOP [closed]使用 PHP OOP 更新博客文章 [关闭]
【发布时间】:2012-12-20 04:05:13
【问题描述】:

我不明白为什么我的博文没有被编辑。我有一个类('blog'),它的方法 update_post() 带有三个参数。这是我的代码(我跳过了连接和其他部分,因为我知道它们正在工作):

<?php
class blog{
function update_post($id, $title, $contents) {
        try {
            $update = $this->db->prepare("UPDATE posts SET title = $title, contents = $contents WHERE id = $id");
            $update->execute();
        }
        catch (PDOException $e) {

        }
    }
}

$post = new blog;

if (isset($_GET['id'])) {
    if (isset($_POST['publish'])) { // If submit button is clicked
        $id = $_GET['id'];
        $title = $_POST['title'];
        $contents = $_POST['contents'];
        $post->update_post($id, $title, $contents);
    }
}
?>

编辑:所以,看来我有多个错误。上面的原始代码来自两个文件,我的 class.blog.php 文件和带有 HTML 表单的页面('edit_post.php')。经过一些实验,我发现错误必须出在edit_post页面。我用“if (1

<?php
if (isset($_GET['id'])) {
    if (isset($_POST['publicera'])) {
        $id = $_GET['id'];
        $title = $_POST['title'];
        $contents = $_POST['contents'];
        $post->update_post($id, $title, $contents);
    }
?>
<form method="post" action="edit_post.php">
    Titel:<br /> 
    <input type="text" name="title" size="80" value="<?php $post->get_title($_GET['id']); ?>"><br />
    Inlägg:<br /><textarea name="contents" rows="20" cols="80"><?php $post->get_contents($_GET['id']); ?></textarea>
    <br /><input type="submit" name="publicera" value="Publicera!">
</form>

<?php
} else {
$post->show_post_list();
}
?>

编辑 #2:已解决!除了错误的 SQL 查询之外,我还需要将表单操作的值修改为 action="edit_post.php?id=&lt;?php echo $_GET['id']; ?&gt;"

【问题讨论】:

  • 如果此代码有任何错误,请在此处提及
  • 我正在上传到我的虚拟主机,但那里永远不会显示错误消息。我想我应该配置我的代码来显示错误。
  • $post = new blogg; 应该是$post = new blog;(一克),对吧?像这样使用准备好的语句也是完全多余的。
  • 您的查询失败,因为变量$title$contents 没有被引用。他们应该是properly bound parameters。否则,您将获得 none 预准备语句提供的安全性,以及可能损坏的查询。
  • @Vienno 不,它不会更新帖子,因为除非你传入了像$title = "'The quoted title'"; 这样用单引号括起来的变量,否则查询是无效 i> 并且会失败。

标签: php sql pdo sql-update


【解决方案1】:

您的查询失败,因为它在语法上无效。这是因为变量$title, $contents, $id 作为不带引号的字符串直接传递到 PDO 准备语句中。您没有获得准备好的语句的任何安全优势,而且实际上您的查询极易受到 SQL 注入和损坏的影响,因为没有引用变量。

它应该使用正确绑定的参数:

function update_post($id, $title, $contents) {
    try {
        // Bind named parameters
        $update = $this->db->prepare("UPDATE posts SET title = :title, contents = :contents WHERE id = :id");
        // Pass the values in an array to execute()
        $update->execute(array(':title' => $title, ':contents' => $contents, ':id' => $id));
    }
    catch (PDOException $e) {
         // Or handle the error, assuming you have PDO setup in ERRMODE_EXCEPTION
         echo "Error in query!!!";
         print_r($this->db->errorInfo());
    }
}

如果您的应用程序的其余部分也使用像您原来的错误查询这样的模式(尽管如果它们有效则正确引用),建议您更新它们以使用上述绑定参数。否则,我们必须假设它们也容易受到 SQL 注入的攻击。

【讨论】:

  • 非常感谢!它没有解决问题,但经过一些实验,至少我知道这个功能正在发挥作用。我一定有另一个错误,我认为它在类实例化部分。请参阅上面编辑过的帖子。
  • @Vienno 我看不到上面的任何编辑。您现在拥有的实例化是正确的。如果您在其他地方遇到问题,请打开 error_reporting。 error_reporting(E_ALL); ini_set('display_errors', 1);
  • 抱歉,现在已编辑。另外,我在远程服务器上运行,所以我无权访问配置文件。但我会尝试和谷歌搜索有关如何打开错误报告的不同解决方案。
  • 谢谢,我现在就试试。
  • POST 数组的 var 转储显示以下内容,但我不知道该怎么做:array(3) { ["title"]=&gt; string(8) "My Title" ["contents"]=&gt; string(16) "Random test text" ["publicera"]=&gt; string(10) "Publicera!" }
猜你喜欢
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多