简体   繁体   中英

how to insert multidimensional array using for loop

I have 2 tables. One is for students and one of subjects. I want to fetch data from the students and subjects tables on same page. In front of student 1 subject 1 subject 2 subject 3. Then in front of student 2 subject 1 subject 2 subject 3. It is to submit result. I have done this. Than I have to insert this in 3rd table of results. I have successfully inserted students in result table. And subjects. But on the time of marks, I am unable to insert. I used a multidimensional array. I am unable to insert marks data into array. How can I do this? Let me show some snapshots. My multidimensional array is not working, and I don't know how to do this. Kindly help me out. This is a detailed pic: This is the detailed snapshot.

         // count students
         $sql = "SELECT * FROM tb_students";
         $run = mysqli_query($mysqli,$sql);
          $total_students = mysqli_num_rows($run);
        $numbers=$total_students;
        for($i=1;$i<=$numbers;$i++)
        {
         while ($rows = mysqli_fetch_assoc($run)) {
            $id = $rows['student_id'];
            $name = $rows['student_name'];

         ?>
        <tr>
            <td>Name</td>
            <td hidden><input type="text" value="<?php echo $id?>" name="student_id[]" /></td>
            <td><?php echo $name ?> </td>
        </tr>
        <input type="hidden" value="<?php echo $numbers;?>" name="numbers" />
        <?php 

            $sel_sub = "SELECT * FROM subjects WHERE class_name = '1st year'";
            $run_sub = mysqli_query($mysqli,$sel_sub);
            $total_sub = mysqli_num_rows($run_sub);
            for ($k=0; $k < $total_sub ; $k++) { 
                while ($rows = mysqli_fetch_assoc($run_sub)) {
                    $sub_id = $rows['sub_id'];
                    $sub_name = $rows['sub_name'];
                ?>  

                    <tr>
                        <td><?php echo $sub_name; ?></td>
                        <td hidden><input type="" value="<?php echo $sub_id;?>" name="sub_id[]" /></td>
                        <input type="hidden" value="<?php echo $total_sub;?>" name="subject" />
                        <td><input type="text" name="marks[][]" placeholder="Marks" /></td>
                    </tr>
                <?php 
                }
            }
         ?>`

and this is isnert query

<?php
$mysqli = mysqli_connect("localhost","salman","salman1214","shan");
if(mysqli_connect_errno())
    die("Connection failed".mysqli_connect_error());
$s = '';
    for($i=0;$i<$_POST['numbers'];$i++)
{
    for($j=0;$j<$_POST['subject'];$j++)
    {
        $s = "insert into result(student_id,exam_name, subject_name, sub_marks) values";
        $s .="('".$_POST['student_id'][$i]."','".$_POST['exam_name']."','".$_POST['sub_id'][$j]."','".$_POST['marks'][$i][$j]."'),";
        $s = rtrim($s,",");
        if(!mysqli_query($mysqli,$s))
    echo mysqli_error();
else
    echo "Records Saved <br />";
        $sub_list = $_POST['marks'][$i][$j];
        echo $sub_list;
    }
}




mysqli_close($mysqli);?>

I don't want to say if this way is the best way.

But, your problem is you are using the lines in the loops which should not. Try this:

<?php
$mysqli = mysqli_connect("localhost","salman","salman1214","shan");
if(mysqli_connect_errno())
    die("Connection failed".mysqli_connect_error());
$s = '';
$s = "insert into result(student_id,exam_name, subject_name, sub_marks) values";
for($i=0;$i<$_POST['numbers'];$i++)
{
    for($j=0;$j<$_POST['subject'];$j++)
    {
        $s .="('".$_POST['student_id'][$i]."','".$_POST['exam_name']."','".$_POST['sub_id'][$j]."','".$_POST['marks'][$i][$j]."'),";
    }
}
$s = rtrim($s,",");
if(!mysqli_query($mysqli,$s))
    echo mysqli_error();
else
    echo "Records Saved <br />";


mysqli_close($mysqli);?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM