doulu8341 2018-11-19 10:29
浏览 90
已采纳

空字段消息无法正确显示

When user trying to edit his blog post and left some fields empty, he should get empty fields error, but, code that should do that get ignored and i get completely different error message instead.

My question: how to display empty fields error message properly?

Code snippets from editpost.php file:

1. Checking for empty fields:

// Check for empty fields
if (empty($title) || empty($body)) {
    // Save correct data into fields
    header('Location: editpost.php?error=emptyeditpostfield&title='.$title.'&body='.$body);
    // Stop script
    exit();
} else {
    $query = "UPDATE posts SET title='$title', body='$body' WHERE id = {$update_id}";
}

2. Desired message:

<!-- Check if user has rights to editing post -->
    <?php if (isset($_SESSION['id'])) : ?>
        <?php if ($_SESSION['name'] == $post['author']) : ?>
            <div class="container">
                <h1>Edit Post</h1>
                <?php 
                    // Message I want to show
                    if (isset($_GET['error'])) {
                        if ($_GET['error'] == 'emptyeditpostfield') {
                            echo '<p class="text-warning">Fill in all fields!</p>';
                        }
                    }
                ?>
                <!-- Edit post form... -->

3. Message I receive:

<!-- /Edit post form... -->
            </div>
        <?php else : header('Location: index.php?error=accessdenied'); exit(); // Message I got ?>
        <?php endif; ?>
    <?php else : header('Location: index.php?error=accessdenied'); exit(); ?>
    <?php endif; ?>

Edited (Nov 30).

4. Code that is a part of this issue as well:

    // Get ID
    $id = mysqli_real_escape_string($conn, $_GET['id']);

    // Create Query
    $query = "SELECT * FROM posts WHERE id = $id";

    // Get Result
    $result = mysqli_query($conn, $query);

    // Fetch Data
    $post = mysqli_fetch_assoc($result);
    // var_dump($posts);

    // Free Result
    mysqli_free_result($result);

    // Close Connection
    mysqli_close($conn);

Picture 1. Warnings after Submit with empty fields.

Warnings

Picture 2. Edit post page.

Edit Post Page

  • 写回答

2条回答 默认 最新

  • doudao7113 2018-12-11 14:40
    关注

    To solve this issue:

    1. In first code snippet need store post id in session as follow:

    // Store post id in session
    session_start();
    $_SESSION['update_id'] = $update_id;
    

    2. In fourth code snippet need check if session has id as follow:

    // Get ID
    if (isset($_SESSION['update_id'])) {
        $id = $_SESSION['update_id'];
    } else {
        $id = $_GET['id'];
    }
    

    Now post id is stored in session and it's possible to retrieve data from database again after redirection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊