Inserting multiple rows from a php form into the database

后端 未结 3 1607
[愿得一人]
[愿得一人] 2021-01-16 19:43

my form as the following table (of textfeild). I am feeding the values into these textfeild. How can i enter these values into my database? plz help.

3条回答
  •  旧巷少年郎
    2021-01-16 20:30

    The name of each field must be different in each row in order to successfully send data from browser to server. In your case for all columns the name remains the same in all rows. You can suffix them with $n

    Solution:

    Replace the code which generate fields to the following:

     for($n=0;$n<$rows;$n++)
        { 
        echo "
        
        
        ";
        }
    

    In above code we have suffixed each field with _$n

    Secondly, Change the code of insert to the following:

    $roll=$_POST['roll_'.$n];
    $name=$_POST['name_'.$n];
    $unit_test=$_POST['unit_test_'.$n];
    $prelims=$_POST['prelims_'.$n];
    $attendance=$_POST['attendance_'.$n];
    $file=$_POST['file_'.$n];
    

    Thirdly, I think the sql query need to be changed. Calculate average before the query and store its values in a variable. Then reference this variable in query same as you have done with other variables like $roll and $name.

    $average=(($unit_test + $prelims)/125) *10;
    

    Now with this code all three lines will be saved in database.

    Hope this helps.

提交回复
热议问题