尝试通过submit按钮更新列值

nhaq1z21  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(336)

我想更新一下 status 通过按“批准/拒绝”按钮在“我的叶”表上。以下是我的表架构:

到目前为止,我已经做到了:

<?php
    if(isset($_POST['approved']))
    {
        echo "Approved";
        $status=$_POST['approved'];
    }
    if(isset($_POST['rejected']))
    {
        echo "Rejected";
        $status=$_POST['rejected'];
    }
?>

    <!-- Begin page content -->
<div class="container">
    <div class="page-header">
        <h3>Employee Leaves</h3>
            <div class="table-responsive">
                <table class="table">
                    <tr>
                        <th>Employee Name</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th>From</th>
                        <th>To</th>
                        <th>Reason</th>
                        <th>Status</th>
                        <th>---</th>
                    </tr>
                <?php
                    include ('database.php');
                    $result = $database->prepare ("SELECT * FROM leaves order by id DESC");
                    $result ->execute();
                    for ($count=0; $row_message = $result ->fetch(); $count++){
                ?>
                    <tr>
                        <td><?php echo $row_message['full_name']; ?></td>
                        <td><?php echo $row_message['phone']; ?></td>
                        <td><?php echo $row_message['email']; ?></td>
                        <td><?php echo $row_message['fromdate']; ?></td>
                        <td><?php echo $row_message['todate']; ?></td>
                        <td><?php echo $row_message['reason']; ?></td>
                        <td><?php echo $row_message['status']; ?></td>
                        <td>
                            <form method="post" action="update.php">
                                <input type="hidden" value="<?php echo $row_message['id']; ?>" />
                                <input type="submit" value="Approved" name="approved"></input>
                               <input type="submit" value="Rejected" name="rejected"></input>
                            </form>
                        </td>
                    </tr>
                    <?php   }   ?>

                    </table>

                    <a href="home"><button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button></a>   
                </div>
            </div>
        </div>  
    </div>
</div>

这是我的update.php

<?php

    $con = mysqli_connect('localhost', 'root', '');
    mysqli_select_db($con, 'leaves');

    $sql = "UPDATE leaves SET status = :status WHERE id = :id";

    if(mysqli_query($con, $sql))
        header("refresh:1; url=view-leave.php");
    else
        echo "Error";
?>

当我点击approve/reject时,我得到了“error”。我想问题出在 update.php . 我不知道是什么,也不知道在哪里。

5jvtdoz2

5jvtdoz21#

必须在此处写入数据库名称:

mysqli_select_db($con, 'dbname');
9jyewag0

9jyewag02#

您是否尝试为 $msg=''; 以及 $status = ''; ?

zpjtge22

zpjtge223#

试试这个: update.php ```

以及 `view-leave.php` ```
<?php
    if(isset($_GET['msg']))
    {
        echo $_GET['msg'];
    }
?>

    <!-- Begin page content -->
<div class="container">
    <div class="page-header">
        <h3>Employee Leaves</h3>
            <div class="table-responsive">
                <table class="table">
                    <tr>
                        <th>Employee Name</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th>From</th>
                        <th>To</th>
                        <th>Reason</th>
                        <th>Status</th>
                        <th>---</th>
                    </tr>
                <?php
                    include ('database.php');
                    $result = $database->prepare ("SELECT * FROM leaves order by id DESC");
                    $result ->execute();
                    for ($count=0; $row_message = $result ->fetch(); $count++){
                ?>
                    <tr>
                        <td><?php echo $row_message['full_name']; ?></td>
                        <td><?php echo $row_message['phone']; ?></td>
                        <td><?php echo $row_message['email']; ?></td>
                        <td><?php echo $row_message['fromdate']; ?></td>
                        <td><?php echo $row_message['todate']; ?></td>
                        <td><?php echo $row_message['reason']; ?></td>
                        <td><?php echo $row_message['status']; ?></td>
                        <td>
                            <form method="post" action="update.php">
                                <input type="hidden" name="id" value="<?php echo $row_message['id']; ?>" />
                                <input type="submit" value="Approved" name="approved"></input>
                               <input type="submit" value="Rejected" name="rejected"></input>
                            </form>
                        </td>
                    </tr>
                    <?php   }   ?>

                    </table>

                    <a href="home"><button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button></a>   
                </div>
            </div>
        </div>  
    </div>
</div>

相关问题