php Undefined offset 1

时间:2015-11-20 09:22:41

标签: php

当我点击提交时,我似乎得到了未定义的偏移量1错误。我一直试图玩这些数字,但似乎没有任何帮助。我在评论的姓氏后面添加了第四列。在添加之前,代码有效。我后来意识到我需要第三列。从那以后我得到一个错误,无法更新sql表。 先感谢您, 阿维

<!DOCTYPE html>
<?php
    echo  '<link rel="stylesheet" type="text/css" href="css/newStyle.css"></head>';
    session_start();

    if (isset($_SESSION['loggedIn']) && ($_SESSION['loggedIn'] == true) && $_SESSION['admin'] == true) {
        echo "<br><h3>Welcome to the administrative area Prof.  " . $_SESSION['firstname'] . "!</h3><br><br>";
    } else {
        //echo "<br>Please log in first to see this page.";
        header  ('Location: index.php');
    }

    require_once 'login.php';
    $connection = new mysqli($hn,$un,$pw,$db);

    if($connection->connect_error) die($connection->connect_error);

    if(isset($_POST['submit'])){
        for($i = 0; $i < $_POST['totalGrades']; $i++){
            echo $i . ': ' . $_POST['grade' .$i]  . '<br>';
            $parts = explode("|", $_POST['grade' .$i]);
            $newGrade = "UPDATE Grades SET grade = '" . $parts[1] . "' WHERE gradeID = " .$parts[0];
            $result = $connection->query($newGrade);
        }
    }

    $username = "";
    $courseId = "";
    $grade = "";
    $courseName = "";
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="">
    </head>
    <body>
        <form method= "post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <link rel="stylesheet" href="css/style.css">
            <?php
                // $courseSct =
                    "SELECT username, courseName, grade, gradeID FROM Courses\n"
                    // . "JOIN Grades\n"
                    // . "ON courses.courseId = Grades.courseId";
                $courseSct = 
                    "SELECT u.firstname, u.lastname, c.courseName, g.grade "
                    . " FROM Grades g "
                    . " INNER JOIN Courses c ON c.courseID = g.courseID "
                    . " INNER JOIN Users u ON u.userID = g.userID "
                    . " WHERE c.professorID = " .$_SESSION['userID'];
                $result = $connection->query($courseSct);
                $rows = $result->num_rows;

                echo 
                    "<table border = '1' width = '50%'>"
                    . "<caption><h2>Grades Table</h2></caption>"
                    . "<tr>"
                    . '<th>First Name</th>'
                    . "<th>Last Name</th>"
                    . "<th>Course Name</th>"
                    . "<th>Grade</th>"
                    //. "<th>New Value</th>"
                    . "</tr>";

                for($j = 0; $j < $rows; ++$j) {
                    $result->data_seek($j);
                    $row = $result->fetch_array(MYSQLI_NUM);

                    echo 
                        "<tr>" . 
                        "<td>" . $row[0] . "</td>" . //First Name
                        "<td>" . $row[1] . "</td>" . //Last Name 
                        "<td>" . $row[2] . "</td>";
                        "<td>"; //Grade

                    echo  '<select name="grade' . $j . '" size="1" id="' . $row[3] . '">';
                    echo '<option value="select">Select</option>';
                    $letterGrade = 'A';
                    for($x = 0; $x < 6; $x++) {         
                        echo '<option value="' . $row[3] . '|' . $letterGrade . '"';
                        if($letterGrade == $row[3]) {
                            echo ' selected';
                        }
                        echo '>' . $letterGrade++ . '</option>';
                    }
                    echo '</select><br>'. "</td>" . "</tr>"; 
                }  
                echo "</table>";
            ?>

            <input type="hidden" name="totalGrades" value="<?php echo $rows;?>">
            <br>
            <input type="submit" name="submit" value="Submit">
            <br>

        </form>

        <a href='index.php?logout'><br>click here to log out<br></a>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您是否尝试过foreach

if(isset($_POST['submit'])){
    foreach($_POST['totalGrades'] as $key => $value) {
        echo $key . ': ' . $value  . '<br>';
        $parts = explode("|", $value);
        $newGrade = "UPDATE Grades SET grade = '" . $parts[1]
                . "' WHERE gradeID = " .$parts[0];
        $result = $connection->query($newGrade);
    }
}