-3

In this code when I want to update the status from Pending to Accepted that time If statement not working. In database I keep Pending by default. Problem is that when I'm going to update the status the STATUS will update but the code inside the If condition can't work. please help me out from this problem. Thanks in advance.

 <?php
include("connect.php");//conection
if(isset($_REQUEST['leave_id']))
{
$id=$_REQUEST['leave_id'];
}
else
{
$id=NULL;
}
$result = mysqli_query($con,"SELECT TAKEN_LEAVES,BALANCE_LEAVE,STATUS 
                              FROM xxxemi_person_leave_t
                              WHERE leave_id= $id");
$res = mysqli_fetch_array($result);
if (!$result) 
  {
  die("Error: Data not found..");
  }
 
 $taken_leave = $res['TAKEN_LEAVES'];
 $balance_leave = $res['BALANCE_LEAVE'];
 $status = $res['STATUS'];

    if(isset($_POST['save']))
          {
    if ($status=='ACCEPTED')
    {
   $status = $_POST['USTATUS'];
   $updated_balance_leave = $_POST['UBALANCE_LEAVE'];

   $updated_balance_leave = $balance_leave - $taken_leave;   
          
   mysqli_query($con,"UPDATE xxxemi_person_leave_t 
                      SET STATUS ='$status', BALANCE_LEAVE ='$updated_balance_leave'
        WHERE leave_id = '$id' "); 

        echo "<script>alert('Your Record Updated');</script>";
       
     }

      else 
    {
     $status = $_POST['USTATUS'];
    
   mysqli_query($con,"UPDATE xxxemi_person_leave_t 
                      SET STATUS ='$status' 
        WHERE leave_id = '$id' "); 

        echo "<script>alert('Your Record Updated');</script>";
       
     }
  }
     
    mysqli_close($con);
?>
/**************PHP CODE *******/
   <form method="post">
             <td><input type="hidden" name="id" value=<?php echo $_GET['leave_id'];?>>
 
             <input type="hidden" name="UBALANCE_LEAVE" value="<?php echo $updated_balance_leave;?>">

              <input type="checkbox"  name="USTATUS" value="ACCEPTED" />
              <img src="images/Approved.png" />&nbsp;&nbsp;&nbsp;

              <input type="checkbox" name="USTATUS" value="REJECTED" />
              <img src="images/Rejected.png" /></td>

              <input type="submit" name="save" value="Update">
            </form>
/************* HTML CODE**********/
  • 2
    You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of concatenating your queries. Specially since you're not escaping the user inputs at all! – M. Eriksson Nov 17 '17 at 06:11
  • _"If statement not working."_ - Not working how? And which if-statement, you have two? – M. Eriksson Nov 17 '17 at 06:12
  • You have a typo: `$statu = $res['STATUS']` and `if ($status=='ACCEPTED')` (you're missing the last `s` on `$status` when you define the variable). – M. Eriksson Nov 17 '17 at 06:14
  • `leave_id = '$id'` - The variable `$id` isn't defined in your example. Is it defined before this snippet? – M. Eriksson Nov 17 '17 at 06:16
  • You're setting the variable `$rl` from the post array, just to completely overwrite it in the next line. – M. Eriksson Nov 17 '17 at 06:18
  • Your `header('location: ....)` would probably throw a _"Headers already sent"_ warning. [Here's why...](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – M. Eriksson Nov 17 '17 at 06:21

1 Answers1

0

It seems that ,you are using $statu = $res['STATUS']; // STATUS $statu for STATUS, but in if condition you are checking for $status


Change This

if ($status=='ACCEPTED')

To

 if($_POST['USTATUS']=='ACCEPTED')

AND type="checkbox" TO type="radio"

helpdoc
  • 1,910
  • 14
  • 36
  • Sir I have posted another code. Please check it and suggest me solution. Thanx & Regards – Arpit Maheshwari Nov 17 '17 at 06:48
  • Print your `$status` before checking, for value stored in `$status` @ArpitMaheshwari – helpdoc Nov 17 '17 at 06:53
  • It shows PENDING before updation . Because it is by default pending in database. when I update the Status it will change but the Updated_balance_leave not update. – Arpit Maheshwari Nov 17 '17 at 06:59
  • because of `$result = mysqli_query($con,"SELECT TAKEN_LEAVES,BALANCE_LEAVE,STATUS FROM xxxemi_person_leave_t WHERE leave_id= $id"); $res = mysqli_fetch_array($result); if (!$result) { die("Error: Data not found.."); } $taken_leave = $res['TAKEN_LEAVES']; $balance_leave = $res['BALANCE_LEAVE']; $status = $res['STATUS'];` code, check this code output first – helpdoc Nov 17 '17 at 07:01
  • OUTPUT: PENDING – Arpit Maheshwari Nov 17 '17 at 07:06
  • Now read this code `if ($status=='ACCEPTED') {` , if your `$status` value is `PENDING' then how this condition will work ? – helpdoc Nov 17 '17 at 07:08
  • Please help me sir. My condition is I want When I change the Status Pending to Accepted that time the balance leave will also update and When I change the Status Pending to Rejected the Balance leave remain the same. – Arpit Maheshwari Nov 17 '17 at 07:14