edit_subject.php无效,错误说数据库查询失败

时间:2015-02-24 10:20:28

标签: php mysql database sql-update

这是我遇到的一种错误:

  

数据库查询失败。

我已上传此网页: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>

1 个答案:

答案 0 :(得分:0)

问题出在其他地方,而不是实际的UPDATE查询。如果您看到发布的链接,则表示您正在使用url传递subject参数,其值为1,即整数。

现在,当您点击提交时,它会将网址更改为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

您需要更正action标记上的<form>参数。

在代码中查找以下行:

<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,这是正确的网址。