如何在PHP中使用嵌套if将数据添加到mysql数据库中?

时间:2016-11-04 12:37:34

标签: php mysql

我的PHP代码存在问题

我创建了两个表。

移动(refno,status_move)

appv_movement_scr(refno,sq_apprv,mv_final,remark,reason,status_move)

我正在尝试根据以下内容将数据添加到两个表中:

序列批准者(sq_apprv)0,1,2。 2号是最终批准者

它还有最终批准者(my_final)0,1。数字1是mv_final的最终批准者。

问题是数据只能在这个if语句中添加。它在嵌套if

时不起作用
<?php
    $connect      = mysqli_connect("localhost", "root", "root", "database");
    global $connect;

    if(isset($_POST['Submit'])){

        $etRefno      = $_POST['refno'];
        $etStaff_id   = $_POST['staff_id'];
        $etRemark     = $_POST['remark'];
        $etReason     = $_POST['reason']; 
        $etApprvBy    = $_POST['apprvby'];
        $etStatus     = "APPROVE";      

        $sql = "SELECT mv_final, user_id, sq_apprv FROM appv_movement_scr WHERE refno='$etRefno' AND apprvby='$etApprvBy'";
        $get = mysqli_query($connect,$sql);
        while($run = mysqli_fetch_assoc($get))
        {
            $final_approver = $run['mv_final'];
            $sq_approver    = $run['sq_apprv'];

            if($final_approver == '1')
            {
                    $result1 = "UPDATE movement SET status_move='$etStatus' WHERE refno='$etRefno'";
                    $run_update = mysqli_query($connect,$result1);
                    $result2 = "UPDATE appv_movement_scr 
                                                SET 
                                                    remark      ='$etRemark', 
                                                    reason      ='$etReason', 
                                                    status_move ='$etStatus'

                                                WHERE refno='$etRefno'";            
                    $run_update = mysqli_query($connect,$result2);
            }
            else
            {
                if($sq_approver == '0')
                {
                    $result1 = "UPDATE movement SET status_move='$etStatus' WHERE refno='$etRefno'";
                    $run_update = mysqli_query($connect,$result1);
                    $result2 = "UPDATE appv_movement_scr 
                                                SET 
                                                    remark      ='$etRemark', 
                                                    reason      ='$etReason', 
                                                    status_move ='$etStatus'

                                                WHERE refno='$etRefno'";            
                    $run_update = mysqli_query($connect,$result2);
                }
                else if($sq_approver == '1')
                {
                    $result1 = "UPDATE movement SET status_move='$etStatus' WHERE refno='$etRefno'";
                    $run_update = mysqli_query($connect,$result1);
                    $result2 = "UPDATE appv_movement_scr 
                                                SET 
                                                    remark      ='$etRemark', 
                                                    reason      ='$etReason', 
                                                    status_move ='$etStatus'

                                                WHERE refno='$etRefno'";            
                    $run_update = mysqli_query($connect,$result2);
                }
                else if($sq_approver == '2')
                {
                    $result1 = "UPDATE movement SET status_move='$etStatus' WHERE refno='$etRefno'";
                    $run_update = mysqli_query($connect,$result1);
                    $result2 = "UPDATE appv_movement_scr 
                                                SET 
                                                    remark      ='$etRemark', 
                                                    reason      ='$etReason', 
                                                    status_move ='$etStatus'

                                                WHERE refno='$etRefno'";            
                    $run_update = mysqli_query($connect,$result2);
                }
                else
                {
                    echo "Duplicate movement";
                }
            }
        }

    }
    $response = array();
    $response["success"] = true; 
    echo json_encode($response);
?>

<form action="approval.php" method="post">  
    <table> 
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Ref no : </td>
            <td><input type ="text" name="refno" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Staff ID : </td>
            <td><input type ="text" name="staff_id" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Remark : </td>
            <td><input type ="text" name="remark" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Reason : </td>
            <td><input type ="text" name="reason" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Approved by : </td>
            <td><input type ="text" name="apprvby" size="30"></td>
        </tr>
    </table>    

    <p><input class="btnSuccess" type ="submit" name="Submit" value="Submit"> </p>              
</form>
有人可以帮忙吗?

感谢。

0 个答案:

没有答案