更新数据库值后删除按钮

时间:2018-03-26 03:35:36

标签: php jquery html

我有一个页面,用户可以查看他们提交的请假申请,他们也可以取消申请,这就是它的样子: View application page

我希望在取消按钮或点击后取消按钮。所以我在执行代码中添加了一个删除按钮脚本。显然,这是错误的。一切正常,直到我在执行代码中添加了脚本。

这是我的表格:

HTML + PHP

<div class="container">
    <div class="page-header">
        <h3>My 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>Type</th>
                    <th>Status</th>
                </tr>
            <?php
                include ('database.php');
                $result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
                $result ->execute();
                for ($count=0; $row_message = $result ->fetch(); $count++){
            ?>
                <tr>
                    <td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></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['type']; ?></td>
                    <td><?php echo $row_message['status']; ?></td>
                    <td>
                        <form method="post" action="update-leave-status-emp.php">
                            <input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
                            <input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
                        </form>
                    </td>
                </tr>
                <?php }?>
            </table>
            <a href="employee_panel.php">
                <button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
            </a>
        </div>
    </div>
</div>

执行代码:

<?php
    if(isset($_POST['cancelled'])){
        $msg = "Cancelled";
        $status=$_POST['cancelled'];
        echo "<script>
            $(document).ready(function(){
                $(".removebtn").click(function(){
                    $(this).remove();
                });
            });
        </script>";
    }
    $leaveno=$_POST['leaveno'];
    $con = mysqli_connect('localhost', 'root', '');
    mysqli_select_db($con, 'companydb');
    $sql = "UPDATE leaves SET status = '$status' WHERE leaveno = '$leaveno'";
    if(mysqli_query($con, $sql))header("refresh:1; url=view-leave-emp.php?msg=$msg");
    else var_dump(mysqli_error($con));
?>
PS:我知道我的代码很容易被注射,我最终会改变它,现在,我必须弄清楚这一点。

1 个答案:

答案 0 :(得分:0)

在if语句中附上已取消的按钮,如果记录被标记为已取消,则按钮将不再显示。

您可以尝试使用此更新代码:

<div class="container">
    <div class="page-header">
        <h3>My 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>Type</th>
                    <th>Status</th>
                </tr>
            <?php
                include ('database.php');
                $result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
                $result ->execute();
                for ($count=0; $row_message = $result ->fetch(); $count++){
            ?>
                <tr>
                    <td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></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['type']; ?></td>
                    <td><?php echo $row_message['status']; ?></td>
                    <td>
                        <form method="post" action="update-leave-status-emp.php">
                            <input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
                          <?php if( $row_message['status'] != "Cancelled" ) {
                            <input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
                          <?php } ?>
                        </form>
                    </td>
                </tr>
                <?php }?>
            </table>
            <a href="employee_panel.php">
                <button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
            </a>
        </div>
    </div>
</div>