在mysql表中插入html表值

pqwbnv8z  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(354)

我想提交一份 html 表值到 mysql table。前两列 html 表是从 mysql'student' 第三栏在网上填写。我想将所有三列插入到中的另一个表中 msyql . 我只是在插入所有表值时遇到了问题。代码如下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>
2mbi3lxu

2mbi3lxu1#

请看下面的更新代码:

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

相关问题