注意:未定义的索引 - PHP错误

时间:2017-02-13 21:52:10

标签: php arrays indexing

我试图按照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';

validation.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]

1 个答案:

答案 0 :(得分:0)

这是因为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()函数代码,因为在此更改后不再需要它。