简体   繁体   English

PHP表格未处理

[英]PHP form not processing

Hi there's a live version of the code below (taken from a tutorial) at my website below http://www.prupt.com/edit_subject.php 嗨,我的网站http://www.prupt.com/edit_subject.php下有以下实时版本的代码(来自教程)

The page has a form that allows you to edit the subjects in the navigation bar down the left hand side. 该页面具有一个表格,可让您在左侧导航栏中编辑主题。 For example, you could click on "About Widget Corp" and the name "About Widget Corp" will appear in the subject text field, at which point your supposed to be able to edit it (ie change its name if you like) then click "edit subject" and it will update the new name in the navigation down the left hand side. 例如,您可以单击“ About Widget Corp”,主题文本字段中将出现名称“ About Widget Corp”,此时您应该可以对其进行编辑(即,如果愿意,可以更改其名称),然后单击“编辑主题”,它将在左侧导航栏中更新新名称。

That's what it's supposed to do, according to the tutorial. 根据教程,这就是应该做的。 However, if I try to edit one of the names, and then click "edit subject" it doesn't change anything. 但是,如果我尝试编辑其中一个名称,然后单击“编辑主题”,则不会进行任何更改。 I'm guessing it's not updating the database and thereafter not outputting the correct/new data to the navigation bar 我猜它没有更新数据库,此后没有将正确/新数据输出到导航栏

Do you see anything in the code below which would explain why it's not updating the navigation bar once I click "edit subject"? 您是否在下面的代码中看到任何可以解释为什么单击“编辑主题”后不更新导航栏的内容?

<?php
//1.Create a database connection
$connection = mysql_connect("98.130.0.87", "username", "password");
if (!$connection) {
    die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("C263430_testorwallo" ,$connection);
if (!$db_select) {
    die("Database selection failed: " . mysql_error());
}

?>

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

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

    $required_fields = array('menu_name', 'position', 'visible');
    foreach($required_fields as $fieldname) {
    if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] !=0)) {
        $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, $connection);
              if (mysql_affected_rows() == 1) {
              //Success
              } else {
              //Failed
              }

    } else {
     // Errors occurred
    }

    } //end:  (isset($_POST['submit']))

?>
<?php find_selected_page();?>
<?php include("includes/header.php"); ?>




<table id="structure">
            <tr>
                <td id="navigation">
                <?php echo navigation($sel_subject, $sel_page); ?>
                </td>
                <td id="page">
                    <h2>Edit Subject <?php echo $sel_subject ['menu_name'];?></h2>
                    <form action="edit_subject.php?subj=<?php echo urlencode($sel_subject['id']);?>" method="post">
                    <p>Subject name: <input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>" id="menu_name" /></p>
                    <p>Position:
                    <select name="position">
                    <?php 
                    $subject_set = get_all_subjects(); 
                    $subject_count = mysql_num_rows($subject_set);
                    //$subject_count +1 because we are adding a subject
                    for($count=1; $count <= $subject_count+1; $count++) {
                    echo "<option value=\"{$count}\"";
                    if ($sel_subject['position'] == $count) {
                    echo " selected";
                    }
                    echo ">{$count}</option>";
                    }
                    ?>

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

                    ?>/> Yes
                    </p>
                    <input type="submit" name"submit" value="Edit Subject"/>
                    </form>                 
                    <br/>
                    <a href="content.php">Cancel</a>
                        </td>
            </tr>
            </table>
<?php include("includes/footer.php"); ?>
<?php
//5. Close connection
mysql_close($connection);
?>

Ok, saw the page code and it's likely that (see comment above). 好的,看到了页面代码,并且很有可能(请参见上面的注释)。

<input type="submit" name"submit" value="Edit Subject"/>

You forgot the = sign, correct it to name="submit" . 您忘记了=号,将其更正为name="submit" That's why it doesn't see the form as submitted (if $_POST['submit']...) 这就是为什么它看不到表单已提交(如果$ _POST ['submit'] ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM