如何在HTML表格中添加更新按钮以编辑表格数据?

时间:2018-10-17 10:33:44

标签: php html sql mysqli

以下代码使用PHP从MySQL表中提取值。我想在此表的最后一列中添加一个更新按钮。我已经做了一些编码,但是当我单击更新时,它显示白屏。我希望当用户单击“更新”按钮时HTML表变得可编辑,并且提交后值将在MySQL表中更改。 这是代码:

<?php

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

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

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

if (!$select_db){
    echo 'Not connected to database';
}

$sql= "SELECT * FROM students";

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

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

if(isset($_POST['update'])){
$UpdateQuery = "UPDATE `students` SET `elected_subject`='$ElectedSubject'";               
mysql_query($UpdateQuery, $con);
};
?>


    <h1><strong>Student Record</strong></h1>
    <table id = "result" class="data-table">
        <caption class="title"></caption>
        <thead>
            <tr>

                <th>Student ID</th>
                <th>Student Name</th>
                <th>Father Name</th>
                <th>Class</th>
                <th>Selected Subject</th>
                <th>View/Update</th>


            </tr>
        </thead>
        <tbody>
        <?php
        $no     = 1;
        $total  = 0;
        while ($row = mysqli_fetch_array($query))
        {
            echo "<form action=update_students.php method=post>";
            $student  = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
            echo '<tr>

                    <td>'.$row['student_id'].'</td>
                    <td>'.$row['student_name'].'</td>
                    <td>'.$row['father_name'].'</td>
                    <td>'.$row['class'].'</td>
                    <td>'.$row['elected_subject'].'</td>
                    <td>' . "<input type='submit' name='update' value='update'>" . '</td>

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

    </table>

update_students.php是具有此代码的同一文件。

0 个答案:

没有答案