【问题标题】:how to insert html table values in mysql table如何在mysql表中插入html表值
【发布时间】:2018-09-26 07:52:13
【问题描述】:

使用以下代码,我从 html 表中的 mysql 表中获取了 Student IDStudent name。在第三列 Obtained Marks 我正在通过输入框获取学生分数。现在我想在新表 testrecord 中插入所有三列(学生 ID、学生姓名和获得的分数)。

<html>
<body>
<?php
$connection = mysqli_connect ('localhost', 'admin', '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">
    <form class="well form-horizontal" action="insert_marks.php" method="post">
    <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>Sr.No.</th>
                <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>'.$no.'</td>
                    <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>

插入标记.php:

<html>
<body>
<?php
$connection = mysqli_connect ('localhost', 'admin', 'password', 'db');
if (!$connection) {
    echo 'Not connected to server';
}

$select_db = mysqli_select_db($connection, 'db');
if (!$select_db) {
    echo 'Not connected to database';
}
//***********Form Submit Goes Here***********//
while 
if($_POST) {
    $student_id     =   $_POST['student_id'];
    $student_name   =   $_POST['student_name'];
    $student_marks  =   $_POST['obtmarks'];

    $sql= "INSERT INTO testrecord (student_id,student_name,obtained_marks) VALUES ('$student_id','$student_name','$student_marks')";
    if (mysqli_query($connection, $sql)) {
    echo "Marks added successfully.";
    echo "<br>";
    echo "<br>";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($connection);
    }
}
mysqli_close($connection);
?>
</body>
</html>

表中有 20 个条目。在文本框中为每个学生插入标记后,上面的编码仅在 mysql 表“testrecord”中插入最后一条记录。您能否更正 insert_marks.php 代码。

【问题讨论】:

    标签: php


    【解决方案1】:

    对 Html 表格 tbody 部分的更改:

    <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>'.$no.'</td>
                    <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>
    

    insert_marks.php 中更改部分:

    //***********Form Submit Goes Here***********//
    while 
    if($_POST) {
        $student_id     =   $_POST['student_id'];
        $student_name   =   $_POST['student_name'];
        $student_marks  =   $_POST['obtmarks'];
    
        for($i = 0; $i < count($student_id); $i++){
            $sql= "INSERT INTO testrecord (student_id,student_name,obtained_marks) VALUES ('$student_id[$i]','$student_name[$i]','$student_marks[$i]')";
            if (mysqli_query($connection, $sql)) {
                 echo "Marks added successfully.";
                 echo "<br>";
                 echo "<br>";
            } else {
                 echo "Error: " . $sql . "<br>" . mysqli_error($connection);
            }
        }
    
    
    }
    

    【讨论】:

    • 它对我有用。但是它给出了“成功添加标记”20次
    • 您可以在for循环之外打印成功消息。然后它只会打印一次。
    • 但是当我从 for 循环中打印成功消息时,它只在 mysql 表 testrecord 中插入最后一个值,即 20th..
    【解决方案2】:

    试试这个

    <html>
    <body>
    <?php
    $connection = mysqli_connect ('localhost', 'admin', '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">
        <form class="well form-horizontal" action="insert_marks.php" method="post">
        <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>Sr.No.</th>
                    <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>'.$no.'</td>
                        <td>'.$row['student_id'].'</td>
                        <input type="hidden" name="student['.$no.'][student_id]" value='.$row['student_id'].'>
                        <td>'.$row['student_name'].'</td>
                        <input type="hidden" name="student['.$no.'][student_name]" value='.$row['student_name'].'>
                        <td>
                            <div class="search-block clearfix">
                                <input name="student['.$no.'][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>
    

    插入标记.php:

    <html>
    <body>
    <?php
    $connection = mysqli_connect ('localhost', 'admin', 'password', 'db');
    if (!$connection) {
        echo 'Not connected to server';
    }
    
    $select_db = mysqli_select_db($connection, 'db');
    if (!$select_db) {
        echo 'Not connected to database';
    }
    //***********Form Submit Goes Here***********//
    
    if(isset($_POST['student']) && !empty($_POST['student'])) {
        $queryStr = '';
        $cnt = count($_POST['student']);
        foreach ($_POST['student'] as $key => $student) {
            $queryStr .= "('".$student['student_id']."','".$student['student_name']."','".$student['obtmarks']."') ";  
            if (($key + 1) !=  $cnt) {
                $queryStr .= " , ";
            }
        }
    
         if ($queryStr != '') {
            $sql= "INSERT INTO testrecord (student_id,student_name,obtained_marks) VALUES $queryStr";
            if (mysqli_query($connection, $sql)) {
            echo "Marks added successfully.";
            echo "<br>";
            echo "<br>";    
         }  
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($connection);
        }
    }
    mysqli_close($connection);
    ?>
    </body>
    </html>
    

    【讨论】:

    • 当我提交分数时它只显示白色窗口。而且它也没有插入标记mysql表'testrecord'
    • 我做了一些更改,请检查一下。也许这对你有帮助:)
    • 添加标记后点击更新时再次显示白屏....您的代码很好,但有一些错误..请您指出...卡在这里很糟糕
    猜你喜欢
    • 2019-06-07
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    相关资源
    最近更新 更多