插入查询在if语句中不起作用

时间:2018-05-05 17:39:28

标签: php mysqli

我试图在一个月内只在数据库中插入一次。如果工资已在本月支付,那么它将显示已经添加。如果没有支付,那么它将在数据库中插入值。 当薪水已经支付了它work.it显示已经支付但是当没有支付时,这个代码显示无效。插入查询在IF语句中不起作用

<form class="form-horizontal" action="add_payment.php" method="post" 
 name="form">
             .
             .
             .

  <div class="form-group">
     <label class="col-sm-5 control-label"></label>
     <div class="col-sm-7">
     <input type="submit" name="submit" value="Pay" class="btn btn-success">
     <a href="home_payment.php" class="btn btn-danger">Cancel</a>
   </div>
</form>

...

<?php

if(isset($_POST['submit'])!="")
{
$emp_id      = $_POST['id'];

$query7  = "SELECT * from payment where emp_id ='".$emp_id."'";
$q7 = $conn->query($query7);
$paidThisMonth = "false";
while($row7 = $q7->fetch_assoc()) {

    date_default_timezone_set("Asia/Dhaka");
    $thisMonth = date("m");
    $paidMonth = date('m', strtotime($row7['pay_date']));

    if ($paidMonth == $thisMonth && $emp_id == $row7['emp_id']) {
        $paidThisMonth = "true";

    }
    else
    {
        $paidThisMonth = "false";
    }
}


if($paidThisMonth == "false")
{

    $emp_id      = $_POST['id'];
    $pay_date      = date("m/d/Y");
    $pay_amount      = $_POST['pay'];
    $pay_method      = $_POST['pay_method'];
    $salary      = $_POST['salary'];
    $due      = $_POST['due'];

    if($due == "0"){
        $pay_status = 0;
    }
    else if($due > "0"){
        $pay_status = 1;
    }
    else if($due == $salary){
        $pay_status = 2;
    }

    $sql = $conn->query("INSERT into payment(emp_id, pay_date, pay_amount, pay_method, pay_status)VALUES('$emp_id','$pay_date','$pay_amount','$pay_method','$pay_status')");

    if($sql)
    {
        ?>
        <script>
            alert('Payment has been successfully added.');
            window.location.href='home_payment.php?page=payment_list';
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
            alert('Invalid.');
            window.location.href='home_payment.php?page=payment_list';
        </script>
        <?php
    }

}
else{

    ?>
    <script>
        alert('Already paid in this month. Try again next month');
        window.location.href='home_payment.php?page=payment_list';
    </script>
    <?php

}

当我在没有任何条件的情况下尝试这个时,它正在工作。

if(isset($_POST['submit'])!="")
{
$emp_id      = $_POST['id'];
    $pay_date      = date("m/d/Y");
    $pay_amount      = $_POST['pay'];
    $pay_method      = $_POST['pay_method'];
    $salary      = $_POST['salary'];
    $due      = $_POST['due'];

    if($due == "0"){
        $pay_status = 0;
    }
    else if($due > "0"){
        $pay_status = 1;
    }
    else if($due == $salary){
        $pay_status = 2;
    }

    $sql = $conn->query("INSERT into payment(emp_id, pay_date, pay_amount, pay_method, pay_status)VALUES('$emp_id','$pay_date','$pay_amount','$pay_method','$pay_status')");

    if($sql)
    {
        ?>
        <script>
            alert('Payment has been successfully added.');
            window.location.href='home_payment.php?page=payment_list';
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
            alert('Invalid.');
            window.location.href='home_payment.php?page=payment_list';
        </script>
        <?php
    }
 }

我想知道问题是什么以及解决方案是什么。

0 个答案:

没有答案