简体   繁体   中英

inserting html table values in mysql table

I am trying to submit a html table values into mysql table. First two columns of html table are fetched from mysql table 'student' and third column is to be filled online. I want to insert all three columns to another table in msyql . I am just facing problem in inserting all table values. code is as under:-

<html>

<body>


<?php

$connection = mysqli_connect ('localhost', 'user', 'password', 'db');

if (!$connection){
    echo 'Not connected to server';
}

$select_db = mysqli_select_db($connection, 'db');

if (!$select_db){
    echo 'Not connected to database';
}
$SelectClass = $_POST ['selectclass'];


$sql= "SELECT * FROM students WHERE class = '$SelectClass'";

$query = mysqli_query($connection, $sql);

if (!$query) {
    die ('SQL Error: ' . mysqli_error($connection));
}
mysqli_close($connection);
?>
 <body>
    <div class="container">

    <h1><strong>Please enter marks of each student for subject</strong></h1>
    <table id = "result" class="data-table">
        <caption class="title"></caption>
        <thead>
            <tr>

                <th>Student ID</th>
                <th>Student Name</th>
                <th>Marks Obtained</th>
            </tr>
        </thead>
        <tbody>
        <?php
        $no     = 1;
        $total  = 0;
        while ($row = mysqli_fetch_array($query))
        {
            $stu  = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
            echo '<tr>

                    <td>'.$row['student_id'].'</td>
                    <td>'.$row['student_name'].'</td>
                    <td>'."<div class='search-block clearfix'><input name='obtmarks' placeholder='' type='number'></div>".'</td>

                </tr>';
            $total += $row['stu_id'];
            $no++;


        }?>
        </tbody>

    </table>
    <button type="submit" class="btn btn-warning" value="insert" align="right">Update<span class="glyphicon glyphicon-send"></span></button>
</form>
</div>
</body>
</html>

Have a look at the updated code below :

<body>


<?php

$connection = mysqli_connect ('localhost', 'user', 'password', 'db');

if (!$connection){
    echo 'Not connected to server';
}

$select_db = mysqli_select_db($connection, 'db');

if (!$select_db){
    echo 'Not connected to database';
}
$SelectClass = $_POST ['selectclass'];


$sql= "SELECT * FROM students WHERE class = '$SelectClass'";

$query = mysqli_query($connection, $sql);

if (!$query) {
    die ('SQL Error: ' . mysqli_error($connection));
}
mysqli_close($connection);

//***********Form Submit Goes Here***********//
if($_POST)
{
    $student_id     =   $_POST['student_id'];
    $student_name   =   $_POST['student_name'];
    $student_marks  =   $_POST['obtmarks'];

    //your code for insertion....

}
?>
 <body>
    <div class="container">

    <h1><strong>Please enter marks of each student for subject</strong></h1>
    <form action="" method="post">
    <table id = "result" class="data-table">
        <caption class="title"></caption>
        <thead>
            <tr>

                <th>Student ID</th>
                <th>Student Name</th>
                <th>Marks Obtained</th>
            </tr>
        </thead>
        <tbody>

        <?php
        $no     = 1;
        $total  = 0;
        while ($row = mysqli_fetch_array($query))
        {
            $stu  = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
            echo '<tr>

                    <td>'.$row['student_id'].'</td>
                    <input type="hidden" name="student_id" value='.$row['student_id'].'>
                    <td>'.$row['student_name'].'</td>
                    <input type="hidden" name="student_name" value='.$row['student_name'].'>
                    <td>'."<div class='search-block clearfix'><input name='obtmarks' placeholder='' type='number'></div>".'</td>

                </tr>';
            $total += $row['stu_id'];
            $no++;


        }?>
        </tbody>

    </table>
    <button type="submit" class="btn btn-warning" value="insert" align="right">Update<span class="glyphicon glyphicon-send"></span></button>
</form>
</div>
</body>
</html>

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