简体   繁体   中英

How to add multiple data in sql table with html and php form

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);

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.

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