douzheng1853 2015-11-20 09:22
浏览 52

php Undefined offset 1

I seem to be getting Undefined offset 1 error when I hit submit. I have been trying to play with the numbers but nothing seems to help. I added a fourth column right after the commented last name. Before the addition the code worked. I realized later on that I needed that third column. Ever since then I get an error and can not update the sql table. Thank you in advance, Avi

<!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
"
                    // . "JOIN Grades
"
                    // . "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条回答 默认 最新

  • dqzlqfqeh845799833 2015-11-20 09:41
    关注

    Have you tried a foreach instead?

    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);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集