1

Hello guys i need some help :/ i'm having a problem with my code.

whenever i try to submit the displayed data only 1 row is being stored in database. and is there a way to store all my data in 1 query?

Here's my code:

<?php

    if (isset($_POST['search'])){

        $dbHost="localhost";
        $dbUser="root";
        $dbPass="";
        $dbName="enrollmentsystem";
        $con = mysqli_connect($dbHost,$dbUser,$dbPass);

        $saccount = $_POST['saccount'];

        mysqli_select_db($con,$dbName);

        $sql = "SELECT * FROM studentaccount WHERE student_number='$saccount'";
        $data = mysqli_query($con,$sql);

        $row = mysqli_fetch_array($data);
        $fsection = $row['section'];

        $sqlano = "SELECT * FROM class WHERE section='$fsection'";
        $dataano = mysqli_query($con,$sqlano);


        while ($scheduledata = mysqli_fetch_array($dataano))
                {   

                    echo "<form action='enroll.php method='POST'><tr>
                    <td><input type='text' name='tnumber' value='{$scheduledata['teacher_number']}'/></td>
                    <td><input type='text' name='cnumber' value='{$scheduledata['class_number']}'/></td>
                    <td><input type='text' name='scode' value='{$scheduledata['subject_code']}'/></td>
                    <td><input type='text' name='section' value='{$scheduledata['section']}'/></td>
                    <td><input type='text' name='room' value='{$scheduledata['room']}'/></td>
                    <td><input type='text' name='days' value='{$scheduledata['days']}'/></td>
                    <td><input type='text' name='start' value='{$scheduledata['start_at']}'/></td>
                    <td><input type='text' name='ends' value='{$scheduledata['ends_at']}'/></td>
                    </tr> </form>";

                }
    }

?>
</table>

<input type="submit" name="enroll" value="Enroll">

<?php

    if (isset($_POST['enroll']))
        {
            $dbHost="localhost";
            $dbUser="root";
            $dbPass="";
            $dbName="enrollmentsystem";
            $con = mysqli_connect($dbHost,$dbUser,$dbPass);

                // if (!$con)
                // {
                //  die("Connection Error" . mysql_error());
                // }

                // echo "Connected Succesfully!";

            mysqli_select_db($con,$dbName);

            $insertQuery = "INSERT INTO grades (class_number,student_number,subject_code,section,teacher_number)

             VALUES('$_POST[cnumber]','$_POST[saccount]','$_POST[scode]','$_POST[section]','$_POST[tnumber]')";

            mysqli_query($con,$insertQuery);

        }
?>
Renz Dizon
  • 11
  • 1
  • 1
    [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – [Jay Blanchard](http://stackoverflow.com/users/1011527/jay-blanchard) – Jeff Puckett Nov 07 '16 at 03:23
  • If you want to store more than one form at once, you'll need to make one form for all the inputs, and make the names contain arrays (just do name="elementname[]" instead of name="elementname") and parse each array in the PHP, more or less like you do when you fetch the arrays from the database. Then loop through the $_POST, and push to database for each result-set. – junkfoodjunkie Nov 07 '16 at 03:30
  • Here's [a pretty similar answer](http://stackoverflow.com/a/40445438/4233593). – Jeff Puckett Nov 07 '16 at 03:34

0 Answers0