PHP Undefined index / variables

我的未来我决定 提交于 2019-12-12 02:42:54

问题


I'm getting an undefined index error with these variables: id, subj, mid, fin. I have defined them properly and I don't know what's wrong with my code. I think the problem is with the placement of the code where I defined those 4 variables. Please help? Thanks.

echo "
    <form action=editgrades.php method=post>
        ID: <input type='text' name='id' maxlength='5' size='3'>
        Subject: <input type='text' name='subj' maxlength='3' size='3'>
        Midterm: <input type='text' name='mid' maxlength='3' size='3'>
        Finals: <input type='text' name='fin' maxlength='3' size='3'>
        <input type='submit' name='submit' value='Update'>
    </form>
";
$mysqli = mysqli_connect("localhost", "root", "", "school");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} else {
    $sql = "select * from studentgrades";
}
$id = $_POST['id'];
$subject = $_POST['subj'];
$midterm = $_POST['mid'];
$finals = $_POST['fin'];
$average = ($midterm + $finals) / 2;
if ($average >= 70) {
    $remarks = 'Passed';
} else {
    $remarks = 'Failed';
}
$res = mysqli_query($mysqli, $sql);
if ($res) {
    $sql1 = "
        update studentgrades set Subject = " . $subject . ",
        Midterm = " . $midterm . ",
        Finals = " . $finals . ",
        Average = " . $average . ",
        Remarks = " . $remarks . "
        where ID = " . $id . "
    ";
    $res1 = mysqli_query($mysqli, $sql1);
    if ($res1) {
        echo "
            Grades updated successfully.
            <br><br>
        ";
    }
}

回答1:


Here's your problem - you don't check if post is set. So if you call this page before submitting a form you will have undefined indices for any $_POST var you try to call.

do something like this:

if(isset($_POST['id']))
{
    $mysqli = mysqli_connect("localhost", "root", "", "school");
    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
    }
    else{
    $sql = "select * from studentgrades";
    }
    $id = $_POST['id'];
    $subject = $_POST['subj'];
    $midterm = $_POST['mid'];
    $finals = $_POST['fin'];
    $average = ($midterm+$finals)/2;
    if($average >= 70){
    $remarks = 'Passed';
    }
    else {
    $remarks = 'Failed';
    }
    $res = mysqli_query($mysqli, $sql);
    if ($res) {
    $sql1 = "
    update studentgrades set Subject = ".$subject.",
    Midterm = ".$midterm.",
    Finals = ".$finals.",
    Average = ".$average.",
    Remarks = ".$remarks."
    where ID = ".$id."
    ";
    $res1 = mysqli_query($mysqli, $sql1);
    if ($res1){
    echo "
    Grades updated successfully.
    <br><br>
    ";
    }
}

Although isset($_POST['id'])) can be substituted with any number of ways to verify the form was submitted.

A side note - you should probably run all of your post processing before any html is output in case you need to modify headers based on the post results. if this code needed to do something like:

if($id == '')
{
    header('location: /');
    exit;
}

it would tell you headers have already been sent and give you another error.




回答2:


It is because you are trying to use the POST variables before the form has been submitted.

Its not complaining about the $id. "Undefined index" means you are trying to use a key of an array that does not exist, which in this case is the $_POST array.

Check the index is set before trying to use them:

$id = isset($_POST['id']) ? $_POST['id'] : null;
$subject = isset($_POST['subj']) ? $_POST['subj'] : null;
etc...

Or alternatively just turn the error reporting down (not recommended)

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);


来源:https://stackoverflow.com/questions/8483844/php-undefined-index-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!