edit_subject.php 不工作,错误是关于数据库查询失败

edit_subject.php is not working, the error is saying about database query failed

这是我遇到的错误类型:

Database query failed.

我上传了这个网页:http://widgetcorp.bugs3.com/public/edit_subject.php?subject=1

这是我的文件:

<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>
<?php find_selected_page(); ?>
<?php 
    if (!$current_subject)
        {
            // subject ID was missing or invalid or
            // subject couldn't be found in database
            redirect_to("manage_content.php");
        }
?>
<?php
    if (isset($_POST['submit']))
    {
        // validations
        $required_fields = array("menu_name", "position", "visible");           
        validate_presences($required_fields);

        $fields_with_max_lengths = array("menu_name" => 30);
        validate_max_lengths($fields_with_max_lengths);

        if (empty($errors))
            {
                // Perform Update

                $id = $current_subject["id"];
                $menu_name = mysql_prep($_POST["menu_name"]);
                $position = (int) $_POST["position"];
                $visible = (int) $_POST["visible"];

                $query  = "UPDATE subjects SET ";
                $query .= "menu_name='{$menu_name}', ";
                $query .= "position={$position}, ";
                $query .= "visible={$visible} ";
                $query .= "WHERE id={$id} ";
                $query .= "LIMIT 1";
                $result = mysqli_query($connection, $query);

                if ($result && mysqli_affected_rows($connection) >= 0)
                    {
                        // Success
                        $_SESSION["message"] = "Subject updated.";
                        redirect_to("manage_content.php");
                    } 
                else
                    {
                        // Failure
                        $message = "Subject update failed.";
                    }
            }
    }           
    // else
        // {
            // // This is probably a GET request
        // }  
?>
<?php include("../includes/layouts/header.php"); ?>
<div id="main">
    <div id="navigation">
        <?php  
             echo navigation($current_subject, $current_page);
        ?>
    </div>
    <div id="page">
        <?php 
            // echo message(); 
            // $message is just a variable, doesn't use the SESSION
            if(!empty($message))
                {
                    echo "<div class=\"message\">" . htmlentities($message) . "</div>";
                }
        ?>
        <?php echo form_errors($errors); ?>
        <h2>Edit Subject: <?php echo htmlentities($current_subject["menu_name"]); ?></h2>

        <form action="edit_subject.php?subject=<?php echo htmlentities($current_subject["menu_name"]); ?>" method="post">

            <p>Menu name:
                <input type="text" name="menu_name" value="<?php echo htmlentities($current_subject["menu_name"]); ?>" />
            </p>

            <p>Position:
                <select name="position">
                <?php
                    $subject_set = find_all_subjects();
                    $subject_count = mysqli_num_rows($subject_set);
                    for ($count=1; $count <= $subject_count; $count++)
                        {
                            echo "<option value=\"{$count}\"";
                            if ($current_subject["position"] == $count)
                                {
                                    echo " selected";
                                }
                            echo ">{$count}</option>";
                        }
                ?>
                </select>
            </p>

            <p>Visible:
                <input type="radio" name="visible" value="0" <?php if ($current_subject["visible"] == 0) { echo "checked"; } ?> /> No
                &nbsp;
                <input type="radio" name="visible" value="1" <?php if ($current_subject["visible"] == 1) { echo "checked"; } ?> /> Yes
            </p>

            <input type="submit" name="submit" value="Edit Subject" />
        </form>
        <br />
        <a href="manage_content.php">Cancel</a>
        &nbsp;
        &nbsp;
        <a href="delete_subject.php?subject=<?php echo urlencode($current_subject["id"]); ?>" onclick="return confirm('Are you sure?');">Delete Subject</a>
    </div>

问题出在其他地方,实际上与您的 UPDATE 查询无关。如果您看到您发布的 link,则您正在传递 subject 参数和 url,其值为 1,它是整数。

现在,当您点击提交时,它会将 url 更改为 http://widgetcorp.bugs3.com/public/edit_subject.php?subject=About%20Widget%20Corp

此处如您所见,subject 参数不是 integer 而是 string 值主题名称。这就是问题所在。

您收到错误消息,因为 ID 类型错误,无法从数据库中正确检索主题数据。您只需要确保表单发布到右侧 url,即 http://widgetcorp.bugs3.com/public/edit_subject.php?subject=1.

为此,您需要更正 <form> 标签上的 action 参数。

在您的代码中查找以下行:

<form action="edit_subject.php?subject=<?php echo htmlentities($current_subject["menu_name"]); ?>" method="post">

并将其更改为

<form action="edit_subject.php?subject=<?php echo htmlentities($current_subject["id"]); ?>" method="post">

如果你看到了,现在表单将提交给http://widgetcorp.bugs3.com/public/edit_subject.php?subject=1,这是正确的url。