0

Could any one plz tell me what's wrong with my code
My sql select while loop is not checking date range that's been inserted into the form.

<?php
if(isset($_POST['start'])&& ($_POST['end'])){
$user_id = $_SESSION['user_id'];
$leave_type= $_POST['leave_type'];
$comments= $_POST['comments'];
$start=date('Y-m-d',strtotime($_POST['start']));
$end=date('Y-m-d',strtotime($_POST['end']));
echo $start,'',$end;
**$checkdate = "SELECT * FROM emp_status WHERE user_id='$_SESSION[user_id]' AND ((start<='$_POST[start]'AND end>'$_POST[start]')
    or
    (start < '$_POST[end]'AND end >='$_POST[end]'))";


$query = mysql_query($checkdate);

$numrows=mysql_num_rows($query );

if ($numrows!=0)
{
while ($row= mysql_fetch_assoc($query))
{
$dbstart=$row ['start'];
$dbend= $row['end'];**
$user=$row ['user_id'];

}
// check to see if they match;
if ($user===$user_id && $dbstart===$start)

{
echo "you already have applied leave this period";

}

else{

    $sql_insert = "INSERT into emp_status
(leave_type,user_id,start,end,comments,status)
  VALUES ('$leave_type','$_SESSION[user_id]','$start','$end','$comments','pending')";
mysql_query($sql_insert) or die("Insertion Failed:" . mysql_error());

}
} 

Edited by ahmadweb: Fixed markdown.

3
Contributors
5
Replies
32
Views
3 Years
Discussion Span
Last Post by ahmadweb
0

Seems like you are missing the quotes in the $_POST associative indexes. And in addition to that to get the $_POST array elements properly parsed withing the double quoted string enclose them within curly brackets.

$checkdate = "SELECT * FROM emp_status 
    WHERE user_id = '{$_SESSION['user_id']}' 
    AND ((start <= '{$_POST['start']}' AND end > '{$_POST['start']}')  
        OR (start < '{$_POST['end']}' AND end >= '{$_POST['end']}'))";

Edited by broj1

0

first part is ok

if(isset($_POST['start'])&& ($_POST['end'])){
    $user_id = $_SESSION['user_id'];
    $leave_type= $_POST['leave_type'];
    $comments= $_POST['comments'];
    $start=date('Y-m-d',strtotime($_POST['start']));
    $end=date('Y-m-d',strtotime($_POST['end']));
    echo $start,'',$end;

second part :

**$checkdate = "SELECT * FROM emp_status WHERE user_id='$_SESSION[user_id]' AND ((start<='$_POST[start]'AND end>'$_POST[start]')
    or
    (start < '$_POST[end]'AND end >='$_POST[end]'))";

--> why you put two * before checkdate var
--> start & end are date variable and $_POST[start] & $_POST[end] are just String so compare operation is not possible => result is always null;
-->i php whene you try to concat var whith somthing else you must operator of concatination ". the dote" ".$_SESSION[user_id]."

i think if you fix this error you will see another error, try to fix it, and your code will run fine

Best regards.

Edited by 203428

0

@203428:

why you put two * before checkdate var

probably just to indicate where the error occurs

start & end are date variable and $_POST[start] & $_POST[end] are just String so compare operation is not possible => result is always null;

If they are properly converted to mysql date format (line 6 and line 7) they can be compared in a query.

i php whene you try to concat var whith somthing else you must operator of concatination ". the dote" ".$_SESSION[user_id]."

What did you mean by that? There is no concatenation only comparison in the line you mentioned.

0

Thanks guys for reply. could you plz give a way to fix this problem. would be thankful
still same result. not checking up the date range stored in my table but keep inserting datee in the table.how about my conditation statements are they fine?

broj1 is right once 'dates string' prperly converted to mysql date format they can be campared in a query.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.