PHP选中的项目无法删除

时间:2012-06-17 15:47:23

标签: php mysql database crud

我目前正在学习如何使用PHP删除,但问题是每次单击处理删除的链接时,都无法删除所选主题。这是我的表格

<?php require_once("./includes/connection.php")?>
<?php require_once("./includes/functions.inc.php"); ?>
<?php

    if(intval($_GET['subj']) == 0){
        redirect_to("content.php");
    }

    if(isset($_POST['submit'])){
        $errors = array();

        //Form Validation
        $required_fields = array('menu_name', 'position', 'visible');
        foreach($required_fields as $fieldname) {
            if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) { 
                $errors[] = $fieldname; 
                }
            }

            $fields_with_lengths = array('menu_name' => 30);
            foreach($fields_with_lengths as $fieldname => $maxlength ) {
                if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; }
            }

            if(empty($errors))
            {
                //Perform Update
                $id = mysql_prep($_GET['subj']);
                $menu_name = mysql_prep($_POST['menu_name']);
                $position = mysql_prep($_POST['position']);
                $visible = mysql_prep($_POST['visible']);

                $query = "UPDATE subjects SET menu_name = '{$menu_name}' , position = {$position}, visible = {$visible} WHERE id = {$id}";

                $result = mysql_query($query,$conn);
                if(mysql_affected_rows() == 1)
                {
                    //Success
                    $message = "The subject was successfully updated.";
                }else{
                    //Failed to Perform Update
                    $message = "The Subject Update Failed";
                    $message .= "<br />" . mysql_error();
                }

            }else{
                //Errors In The Field
                $message = "There were " . count($errors) . " Error in the form";
            }

    }//End of Issset Subject if
?>
<?php find_selected_page(); ?>
<?php include("includes/header.inc.php"); ?>
<table id="structure">
    <tr>
        <td id="navigation">
            <ul class = "subjects">
                <?php echo navigation($sel_subj,$sel_page)?>
        </td>
        <td id="page">
                <h2>Edit Subject: <?php echo "{$sel_subj['menu_name']}"; ?></h2>
                <?php if(!empty($message)){
                    echo "<p class = \"message\"> ". $message . "</p>";
                }?>

                <form action = "edit_subject.php?subj=<?php echo urlencode($sel_subj['id']);?>" method = "POST">
                    <p>Subject Name:
                        <input type = "text" name = "menu_name" value = "<?php echo "{$sel_subj['menu_name']}";?>">
                        <?php if(!empty($errors) && in_array('menu_name',$required_fields)){ ?>
                            <span class = "error">Please Enter Your Menu Name</span>
                        <?php } ?>
                        </br>
                    </p>

                    <p>Position
                        <select name = "position">
                            <?php 
                                $subject_set  = get_all_subjects();
                                $subject_count = mysql_num_rows($subject_set);

                                for($count = 1; $count<= $subject_count+1;$count++)
                                {
                                    //$subject_count+1 b/c we're adding a new Subject
                                    echo "<option value =\"{$count}\"";
                                    if($sel_subj['position'] == $count)
                                    {
                                        echo " selected";
                                    }
                                    echo "> {$count} </option>";
                                }
                            ?>
                        </select>
                    </p> 

                    <p> Visible:
                        <input type = "radio" name = "visible" value = "0" 
                            <?php 
                                if($sel_subj['visible'] == 0)
                                {
                                    echo " checked";
                                }
                            ?>
                        />No &nbsp
                        <input type = "radio" name = "visible" value = "1" 
                            <?php 
                                if($sel_subj['visible'] == 1)
                                {
                                    echo " checked";
                                }
                            ?>
                        />Yes
                    </p>
                    <input type = "submit" name = "submit" value = "Edit Subject"> <span>
                    &nbsp; &nbsp;
                    <a href="delete_subject.php?subj=<?php echo urlencode($sel_subject['id']); ?>" onclick="return confirm('Are you sure?');">Delete Subject</a>
                </form>

                <br/>
                <a href ="content.php">Cancel</a>
        </td>
    </tr>
</table>
<?php require("includes/footer.inc.php"); ?>

这是delete_subject.php

<?php require_once("./includes/connection.php")?>
<?php require_once("./includes/functions.inc.php"); ?>

<?php 
        if (intval($_GET['subj']) == 0) {
            redirect_to("content.php");
        }

        $id = mysql_prep($_GET['subj']);



            $query = "DELETE FROM subjects WHERE id = {$id}";
            $result = mysql_query($query, $conn);
            if (mysql_affected_rows() == 1) {
                redirect_to("content.php");
            } else {
                // Deletion Failed
                echo "<p>Subject deletion failed.</p>";
                echo "<p>" . mysql_error() . "</p>";
                echo "<a href=\"content.php\">Return to Main Page</a>";
            }
?>

<?php mysql_close($connection); ?>

这是处理删除页面的行

<a href="delete_subject.php?subj=<?php echo urlencode($sel_subject['id']); ?>" onclick="return confirm('Are you sure?');">Delete Subject</a>

为什么我的删除请求无法处理?

0 个答案:

没有答案