尝试通过提交按钮更新列值

时间:2018-01-19 06:09:08

标签: php html mysql database phpmyadmin

我正在尝试按下批准/拒绝按钮更新我的离职表上的status。这是我的表架构:

Table Schema

到目前为止,这就是我所做的:

<?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";
?>

当我点击批准/拒绝时,我收到“错误”。我认为问题出在update.php上。我不确定它是什么或在哪里。

3 个答案:

答案 0 :(得分:0)

试试这个:update.php

<?php
    if(isset($_POST['approved']))
    {
        $msg = "Approved";
        $status=$_POST['approved'];
    }
    if(isset($_POST['rejected']))
    {
        $msg = "Rejected";
        $status=$_POST['rejected'];
    }
    $id=$_POST['id'];
    $con = mysqli_connect('localhost', 'root', '');
    mysqli_select_db($con, 'leaves');

    $sql = "UPDATE newusers SET username = '$status' WHERE id = '$id'";

    if(mysqli_query($con, $sql))
        header("refresh:1; url=index.php?msg=$msg");
    else
        var_dump(mysqli_error($con));
    ?>

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>

答案 1 :(得分:0)

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

mysqli_select_db($con, 'dbname');

答案 2 :(得分:0)

您是否尝试为$msg='';$status = '';声明默认值?