与更新用户一起使用时,更新帖子不起作用

时间:2019-07-08 21:59:44

标签: php mysql

我正在创建一个图像发布网站,必须在其中放置更新发布选项,但是尝试了许多之后,我不知道发生了什么,我已经检查了查询,一切正常。

我正在使用php7和mysql数据库。

在这里我触发编辑帖子并更新帖子选项

if (isset($_GET['edit-post'])) {
    $isEditingPost = true;
    $post_id = $_GET['edit-post'];
    editPost($post_id);
}
// if user clicks the update post button
if (isset($_POST['update_post'])) {
    updatePost($_POST);
}

我的更新后代码在这里

function updatePost($request_values)
    {
global $conn, $errors, $post_id, $title, $featured_image, $topic_id, $published;

                $title = esc($request_values['title']);
        //$body = esc($request_values['body']);
        $post_id = esc($request_values['post_id']);
        if (isset($request_values['topic_id'])) {
            $topic_id = esc($request_values['topic_id']);
        }
        // create slug: if title is "The Storm Is Over", return "the-storm-is-over" as slug
        $post_slug = makeSlug($title);

        if (empty($title)) { array_push($errors, "Post title is required"); }
        //if (empty($body)) { array_push($errors, "Post body is required"); }
        // if new featured image has been provided
        if (isset($_POST['featured_image'])) {
            // Get image name
            $featured_image = $_FILES['featured_image']['name'];
            // image file directory
            $target = "../static/images/" . basename($featured_image);
            if (!move_uploaded_file($_FILES['featured_image']['tmp_name'], $target)) {
                array_push($errors, "Failed to upload image. Please check file settings for your server");
            }
        }

        // register topic if there are no errors in the form
        if (count($errors) == 0) {
            $query = "UPDATE posts SET title='$title', slug='$post_slug', image='$featured_image', published=$published, updated_at=now() WHERE id=$post_id";
            // attach topic to post on post_topic table
            if(mysqli_query($conn, $query)){ // if post created successfully
                if (isset($topic_id)) {
                    $inserted_post_id = mysqli_insert_id($conn);
                    // create relationship between post and topic
                    $sql = "INSERT INTO post_topic (post_id, topic_id) VALUES($inserted_post_id, $topic_id)";
                    mysqli_query($conn, $sql);
                    $_SESSION['message'] = "Post created successfully";
                    header('location: posts.php');
                    exit(0);
                }
            }
            $_SESSION['message'] = "Post updated successfully";
            header('location: posts.php');
            exit(0);
        }
    }

我在这里提交我的代码

    <button type="submit" class="btn btn-primary" name="update_post">UPDATE</button>

我从Here那里获得了推荐

=============错误=============================

Notice: Undefined index: post_id in D:\newphp\htdocs\social\admin\includes\post_functions.php on line 169

Fatal error: Uncaught TypeError: Argument 1 passed to esc() must be of the type string, null given, called in D:\newphp\htdocs\social\admin\includes\post_functions.php on line 169 and defined in D:\newphp\htdocs\social\admin\includes\admin_functions.php:301 Stack trace: #0 D:\newphp\htdocs\social\admin\includes\post_functions.php(169): esc(NULL) #1 D:\newphp\htdocs\social\admin\includes\post_functions.php(69): updatePost(Array) #2 D:\newphp\htdocs\social\admin\create_post.php(4): include('D:\\newphp\\htdoc...') #3 {main} thrown in D:\newphp\htdocs\social\admin\includes\admin_functions.php on line 301

esc函数在线:301

function esc(String $value){
    // bring the global db connect object into function
    global $conn;
    // remove empty space sorrounding string
    $val = trim($value); 
    $val = mysqli_real_escape_string($conn, $val);
    return $val;
}```

0 个答案:

没有答案