【问题标题】:Notice: Undefined index - PHP Error注意:未定义的索引 - PHP 错误
【发布时间】:2017-02-13 21:52:10
【问题描述】:

我正在尝试按照 Lynda 的 PHP 教程进行操作,但我坚持使用 CRUD 页面的验证。我一直在试图弄清楚出了什么问题,但我似乎无法找到错误的来源,即使它已经被错误地说明了。我很困惑,因为我使用相同的验证来创建主题并且它执行得很好,但是在创建页面时,我得到了错误 - Notice: Undefined index: visible in D:\wd-stuff\ampps\www\cms.widgetcorp\includes\validation.php on line 14

下面是我正在处理的完整代码。

PS:我有一个名为 visible 的无线电输入。

PS2:我已经阅读了解决未定义索引的链接,但它没有回答我的问题。同样的验证在另一个表单(new_subject.php)上也能正常工作,它与 new_page.php 基本相同。

new_page.php

    require_once '../includes/session.php';
    require_once '../includes/functions.php';
    require_once '../includes/connection.php';
    require_once '../includes/validation.php';

    getSelectedContent();

    if (!$is_subject) {
        redirect_to("manage_content.php");
    }

    if (isset($_POST['submit'])) {

        // validations

        $req_fields = array("menu_name", "position", "visible", "content");
        validatePresence($req_fields);
        $field_with_length = array ("menu_name" => 30);
        validateLenght($field_with_length);


        if (empty($errors)) {

            $s_id = $is_subject['id'];
            $menu_name = mysqlPrep($_POST['menu_name']);
            $position = (int) $_POST['position'];
            $visible = (int) $_POST['visible'];
            $content = mysqlPrep($_POST['content']);

            $qry  = "INSERT INTO pages (";
            $qry .= " subject_id, menu_name, position, visible, content";
            $qry .= ") VALUES (";
            $qry .= " {$s_id}, '{$menu_name}', {$position}, {$visible}, '{$content}'";
            $qry .= ")";
            $result = mysqli_query($conn, $qry);

            if ($result) {
                $_SESSION['msg'] = "Page added.";
                redirect_to("manage_content.php?subject=" . urlencode($is_subject['id']));
            } else {
                $_SESSION['msg'] = "Page Creation Failed";
            }

        }
    } 

    include_once '../includes/layouts/header.php';

验证.php

$errors = array();

function isDataPresent($value)
{
    return (isset($value) && $value !== "");
}

function validatePresence($fields)
{
    global $errors;

    foreach ($fields as $field) {
        $value = trim($_POST[$field]);
        if (!isDataPresent($value)) {
            $errors[$field] = str_replace("_", " ", ucfirst($field)) . " can't be blank";
        }
    }   
}

function isLengthValid($value, $max)
{
    return (strlen($value) <= $max);
}

function validateLenght($fields)
{
    global $errors;
    foreach ($fields as $field => $max) {
        $value = trim($_POST[$field]);
        if (!isLengthValid($value, $max)) {
            $errors[$field] = str_replace("_", " ", ucfirst($field)) . " is too long";
        }
    }
}

我也想过将两者结合起来。但我的帖子的主要目标是了解为什么验证适用于一种形式,但不适用于基本相同的另一种形式。

new_subject.php

require_once '../includes/session.php';
require_once '../includes/functions.php';
require_once '../includes/connection.php';
require_once '../includes/validation.php';

if (isset($_POST['submit'])) {
    $menu_name = mysqlPrep($_POST['menu_name']);
    $position = (int) $_POST['position'];
    $visible = (int) $_POST['visible'];

    // validations

    $req_fields = array("menu_name", "position", "visible");
    validatePresence($req_fields);
    $field_with_length = array ("menu_name" => 30);
    validateLenght($field_with_length);


    if (!empty($errors)) {
        $_SESSION['errors'] = $errors;
        redirect_to('new_subject.php');
    }

    $qry  = "INSERT INTO subjects (";
    $qry .= "menu_name, position, visible";
    $qry .= ") VALUES (";
    $qry .= " '{$menu_name}', $position, $visible";
    $qry .= ")";
    $result = mysqli_query($conn, $qry);

    if ($result === false) {
        $_SESSION['msg'] = "Subject Creation Failed";
        redirect_to("new_subject.php");
    }

    $_SESSION['msg'] = "Subject Created";
    redirect_to("manage_content.php");

} else {
    redirect_to("new_subject.php");
}

主题形式

<form action="create_subject.php" method="post">
            <p>Menu name: 
                <input type="text" name="menu_name" value="">
            </p>
            <p>Position: 
                <select name="position">
                <?php
                    $subject_set = getAllSubjects();
                    $subject_cnt = mysqli_num_rows($subject_set);
                    for ($cnt = 1; $cnt <= $subject_cnt + 1; $cnt++) {
                        echo "<option value=\"{$cnt}\">{$cnt}</option>";
                    }
                ?>
                </select>
            </p>
            <p>Visible: 
                <input type="radio" name="visible" value="0"> No
                &nbsp;
                <input type="radio" name="visible" value="1"> Yes
            </p>
            <input type="submit" name="submit" value="Create Subject">
        </form>

页面表单

<form action="new_page.php?subject=<?= urlencode($is_subject['id']); ?>" method="post">
            <p>Menu name: 
                <input type="text" name="menu_name" value="">
            </p>
            <p>Position: 
                <select name="position">
                <?php
                    $page_set = getAllPages($is_subject['id']);
                    $page_cnt = mysqli_num_rows($page_set);
                    for ($cnt = 1; $cnt <= $page_cnt + 1; $cnt++) {
                        echo "<option value=\"{$cnt}\">{$cnt}</option>";
                    }
                ?>
                </select>
            </p>
            <p>Visible: 
                <input type="radio" name="visible" value="0"> No
                &nbsp;
                <input type="radio" name="visible" value="1"> Yes
            </p>
            <p>Content: <br>
                <textarea rows="20" cols="80" name="content"></textarea>
            </p>
            <input type="submit" name="submit" value="Create Page">
        </form>[Validation error that I'm trying to reproduce][1]

【问题讨论】:

标签: php arrays indexing


【解决方案1】:

这是因为 validatePresence() 函数中的一个错误,该函数尝试根据条件检查一个值,而该值无法设置,因此引发了 E_NOTICE。将该函数的代码更改为:

function validatePresence($fields)
{
    global $errors;

    foreach ($fields as $field) {
        if (!isset($_POST[$field]) || trim($_POST[$field]) == '') {
            $errors[$field] = str_replace("_", " ", ucfirst($field)) . " can't be blank";
        }
    }   
}

然后,您可以安全地删除 isDataPresent() 函数代码,因为在此更改之后不再需要它。

【讨论】:

  • 感谢您的回复。我已经编辑了我的帖子并添加了我正在谈论的其他表单处理。
  • 我明白了。您可以通过发布您提到的 2 个表单的 HTML 来更新您的问题吗?一个有效,一个抛出错误。
  • 主题表单是有效的。页面表单是我收到错误的地方。
  • 您是否按照我的建议更改了 validatePresence() 代码,但您仍然收到错误消息?另外,在提交之前,您是否选择了页面表单中的两个单选输入之一?
  • 我没有更改函数,因为我试图理解为什么它不会像使用其他表单一样验证字段。不,我没有选择任何无线电输入,因此它应该返回一条错误消息“可见字段不能为空白”,就像主题表单一样。但由于某种原因,相同的验证不适用于页面表单。
猜你喜欢
  • 2011-05-26
  • 1970-01-01
  • 2013-03-20
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
相关资源
最近更新 更多