简体   繁体   English

为什么我的UPDATE sql查询不起作用? 不会更新表

[英]Why isn't my UPDATE sql query working? Will not update table

Is there something wrong with my function here? 我的功能有问题吗? I've created a simple task manager with a blog page as well. 我还创建了一个带有博客页面的简单任务管理器。 This is the same function I use to update Tasks in the task table, just modified to update the Blog table instead. 这与我用来更新任务表中的“ task功能相同,只是为了更新Blog表而进行了修改。

function update_blog($blogpost) {
    global $db;

    $sql = "UPDATE Blog SET ";
    $sql .= "blog_date='" . db_escape($db, $blogpost['blog_date']) . "', ";
    $sql .= "blog_content='" . db_escape($db, 
    $blogpost['blog_content']) . "' ";
    $sql .= "WHERE id='" . db_escape($db, $blogpost['id']) . "' ";
    $sql .= "LIMIT 1";

    $result = mysqli_query($db, $sql);

    if($result) {
      return true;
    } else {
      // UPDATE failed
      echo mysqli_error($db);
      db_disconnect($db);
      exit;
    }
}

From edit.php , the user is redirected to blog.php upon submitting their blog post edits. edit.php ,用户会被重定向到blog.php在提交自己的博客文章编辑。 However, the edits are not seen and the table itself is not updated. 但是,看不到编辑,并且表本身未更新。 Am I missing something simple here? 我在这里缺少简单的东西吗?

It might have something to do with a record with db_escape($db, $blogpost['id']) not being present in the database (if the query is not failing). 它可能与数据库中不存在db_escape($db, $blogpost['id'])的记录有关(如果查询没有失败)。 I would recommend executing a simple SELECT to see how many rows exist, eg: 我建议执行一个简单的SELECT来查看存在多少行,例如:

SELECT * 
FROM Blog
WHERE id = <value of db_escape($db, $blogpost['id'])>;

If you don't get any rows, that probably means there is no record present and hence, no update. 如果没有任何行,则可能意味着没有记录存在,因此也就没有更新。 You might have to pass different value in that case, depending on what you have in the form. 在这种情况下,您可能必须传递不同的值,具体取决于表单中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM