-1

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);?>
wogsland
  • 9,106
  • 19
  • 57
  • 93
shinzu
  • 39
  • 5
  • 1
    Please post any relevant code within the question as text, not as screenshots. – cteski Feb 13 '17 at 18:22
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Jonathan Kuhn Feb 13 '17 at 18:24
  • means? dont understand – shinzu Feb 13 '17 at 18:26
  • @JonathanKuhn check coede i have pasted – shinzu Feb 13 '17 at 18:28
  • @cteski check the code please – shinzu Feb 13 '17 at 18:30
  • 1
    You should edit your original question to add code, not post answers. SO isn't a discussion forum so you don't "reply" to your question. It is a question and answer site so you ask a question, update/edit that question and people post answers to the question. And typically you don't post all your code, you post an [mcve]. No one wants to read through all your code and it is likely that if you break it down to a small example you could find the problem yourself. – Jonathan Kuhn Feb 13 '17 at 18:32
  • 1
    Next, in the image there are a bunch of errors. I posted to another question that answers how to solve those errors. If you fix that, it would solve your problem which is why I posted it and marked the question as a duplicate. If you would `print_r($_POST)` it will give you the full structure of the array which should give you an idea of how to access any key/value. – Jonathan Kuhn Feb 13 '17 at 18:34
  • @JonathanKuhn this is not solving my issue. kindly wolud u like to help me that how to store data into multidimensional array from input fields? – shinzu Feb 13 '17 at 18:48
  • updated my question. kindly now tell me what to do – shinzu Feb 13 '17 at 19:06

1 Answers1

0

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);?>
Mojtaba
  • 4,852
  • 5
  • 21
  • 38