用手风琴的php按钮更新数据库

时间:2017-03-20 00:18:26

标签: php html mysql

我有以下代码,用不同的手风琴显示db表中的条目。在这些手风琴中,我已经包含了用于改变某些行条目的按钮,但在这种情况下我真的不知道如何启动它。

这是代码

<?php
                $sql = "SELECT formid, username, formstatus  FROM forms";
                if(!empty($_POST['action']) && !empty($_POST['form_id'])){
                $action = $_POST['action'];
                $formId = $_POST['form_id'];

                $newStatus = null;

                if($action == 'accept_form'){
                    $newStatus = "Accepted";
                } else if($action = 'deny_form') {
                    $newStatus = "Denied";
                } else {
                    //Action is neither accept_form or deny_form
                }

                if($newStatus !== null){
                $updateFormStatus = $con->prepare('UPDATE `nopforms` SET `nopstatus` = ? WHERE `id` = ?');
                if($updateFormStatus !== false){
                $updateFormStatus->bind_param('si', $newStatus, $formId);  
                    if($updateFormStatus->execute()){
                echo "Success";
            } else {
                //Error has occurred when updating form status.
                echo "Fail";
            }
        } else {
            print_r($con->error_list); //It is just for debug purposes, shouldn't be used in production. It is not a good idea to show raw errors to user.
        }
    }
}
                $result = $con->query($sql);
                $i = 0; 
        if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
        $i++;
                $nopid = $row["nopid"];
                $username = $row["username"];
                $nopstatus = $row["nopstatus"];


                echo "<div class='pnel-group' id='accordion".$i."'>
                <div class='panel panel-default'>
                    <div class='panel-heading'>
                        <h4 class='panel-title'>
                            <a data-toggle='collapse' data-parent='#accordion".$i."' 
                                    href='#collapse".$i."'>Form ID: ".$formid." | Student Name: ".$studentname."
                            </a>
                        </h4>
                    </div>
                    <div id='collapse".$i."' class='panel-collapse collapse'>
                        <div class='panel-body'>                        
                        <b>Username: </b>".$username."<br>
                        <b>Form status: </b>".$nopstatus."<br>


                        </div>
                        <hr>
                        <div class='panel-footer'>
                         <form method='post'>
                         <input type='hidden' name='nop_id' value='".$nopid."'>
                            <button class='btn btn-success' type='submit' name='action' value='accept_form'>Accept Form</button>
                            <button class='btn btn-danger' type='submit' name='action' value='deny_form'>Deny Form</button>
                        </form>

                    </div>

                </div>
                <hr>
            </div>

                ";
                }
                } else {
                echo "There aren't any NoP's submitted!";
                }


                ?>

我想要实现的是当用户按下deny或accept按钮来更改指定手风琴中$ formstatus行中的值时。 谢谢你的时间

1 个答案:

答案 0 :(得分:0)

您可以让按钮发送包含您要修改 fomrstatus 形式的 id 的表单,而按钮本身可以是一个动作,接受或拒绝形式。

它可能看起来像这样(请记住它是php字符串的一部分,我在那里嵌入 $ formid 变量):

<div class='panel-footer'>
    <form method='post'>
        <input type='hidden' name='form_id' value='".$formid."'>
        <button class='btn btn-success' type='submit' name='action' value='accept_form'>Accept Form</button>
        <button class='btn btn-success' type='submit' name='action' value='deny_form'>Deny Form</button>
    </form>
</div>

然后在从数据库中选择行之前,所以在$result = $con->query($sql);之前,您需要检查是否有要处理的操作:

if(!empty($_POST['action']) && !empty($_POST['form_id'])){
    $action = $_POST['action'];
    $formId = $_POST['form_id'];

    $newStatus = null;

    if($action == 'accept_form'){
        $newStatus = 1;
    } else if($action = 'deny_form') {
        $newStatus = 0;
    } else {
        //Action is neither accept_form or deny_form
    }

    if($newStatus !== null){
        $updateFormStatus = $con->prepare('UPDATE `forms` SET `formstatus` = ? WHERE `id` = ?');
        if($updateFormStatus !== false){
            $updateFormStatus->bind_param('ii', $newStatus, $formId);  
            if($updateFormStatus->execute()){
                //Form status has been updated successfuly.
            } else {
                //Error has occurred when updating form status.
            }
        } else {
            var_dump($con->errno, $con->error); //It is just for debug purposes, shouldn't be used in production. It is not a good idea to show raw errors to user.
        }
    }
}

所以想法是检查两个参数是否存在,然后检查它是否是accept_form或deny_form并为formstatus准备值。

我在那里使用了整数但你可以使用字符串,如果你真的想要它,但如果你想只选择接受/拒绝的表格,那么将列值与整数然后字符串进行比较可能会更快。

然后我检查是否已准备好新状态(严格来说不是null)(如果action既不是accept_form也不是deny_form,那么它将不会被准备好并保持为null),并准备更新语句然后执行它。

然后从表单中选择行并在更新时构建html。