0

Hello I am beginner here. I have a record like this in my SQL Table

id    name_id    score
1     N_1         98
2     N_3         78
3     n_3         68

I have a HTML and PHP set of code that display a table with an input fields next to each of its ID's and Name which looks like
CONSIDER [ ] AS INPUT FIELDS

ID     NAME       SCORE
N_4    James      []
N_5    Kit        []
N_6    Chino      []

When I put some scores into it just like

ID     NAME       SCORE
N_4    James      [98]
N_5    Kit        [76]
N_6    Chino      [81]

My SQL table should look like this

id    name_id    score 
1     N_1         98
2     N_2         78
3     N_3         68
4     N_4         98
5     N_5         76
6     N_6         81

But instead of inserting 3 rows of data in my SQL Table it just insert as one, just look like this

id    name_id    score 
1     N_1         98
2     N_2         78
3     N_3         68
4     N_4         98

What should I do? Here is some of my code

<?php $sql = "SELECT l_name, f_name, m_name, sid FROM tbl_student WHERE section='".$_POST['section']."';"; ?>
                        <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>NAME</th>
                                <th>Score</th>
                            </tr>    
                        </thead>
                        <tbody>
                                <?php $result = mysqli_query($db, $sql);
                                    while ($resultset = mysqli_fetch_assoc($result)){ ?>
                                    <tr>
                                    <form method="post">
                                    <td><?php echo $resultset ['sid']; ?></td>
                                    <td style="text-transform:capitalize;"><?php echo $resultset ['l_name']; echo ", "; echo $resultset ['f_name']; echo " "; echo $resultset ['m_name'];?></td>
                                    <td><input type="text" style="width:50px;" name="grade"></td>
                                    <input type="hidden" name="id" value="<?php echo $resultset ['sid']; ?>">
                                    </tr>
                                <?php } ?>
                        </tbody>
                        </table> 
                                    <button class="pull-right btn btn-info" type="submit" name="submit_tbl_grade">SAVE</button>
                                    </form>
                    <?php } ?>
                        <?php if(isset($_POST['submit_tbl_grade'])){
                            $name = $_POST['grade'];
                            $id =   $_POST['id'];
                            $record = "INSERT INTO tbl_grade(stud_id,component_value) VALUES ($id,$name)";
                            mysqli_query($db,$record);
2
  • you have only one insert statement, and its not in a loop, therefore there is only one insert. If you use a prepared statement you can execute it with the other values. You'll also need to give each input in the loop a different name. Commented Oct 23, 2018 at 4:48
  • i got it. Thank you that was so useful :) Commented Oct 23, 2018 at 6:30

1 Answer 1

1

Change your input tags like below.

<input type="text" style="width:50px;" name="grade[]">
<input type="hidden" name="id[]" value="<?php echo $resultset ['sid']; ?>">

Then you can access the values like below,

$name = $_POST['id'][0]
$id = $_POST['grade'][0]

Using a loop you can access the values and insert them to DB.

Sign up to request clarification or add additional context in comments.

1 Comment

Its working. Thank you, it was so useful. that helped me a lot :) Godbless

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.