0

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\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>
Szabolcs Páll
  • 1,402
  • 6
  • 25
  • 31
Avi
  • 29
  • 1
  • 11
  • 1
    Edit the question to include the $_POST data that you submit. – Andrius Nov 20 '15 at 09:23
  • Where is your `grade . $i` (e.g. grade1) defined? I cannot see it in the code.. – Can O' Spam Nov 20 '15 at 09:31
  • What do you get if you var dump $parts? – Tim Sheehan Nov 20 '15 at 10:12
  • Beware! You are not escaping your SQL query variables, so they could easily be subject to SQL injection attacks. See also [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) for more info on how to fix this. – Simba Nov 20 '15 at 10:14

1 Answers1

0

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);
    }
}
Christian
  • 1,557
  • 11
  • 16