0

I am facing one issue while issuing below query. I am not sure what am i doing wrong?

$start= date("y-m-d", strtotime($start));        
$end= date("y-m-d", strtotime($end));
//    $start = '2016-03-03';
//   $end= '2016-04-04';
if($jobtitle == ''){
    $jobtitle = true;
}
if($start == '' or $end == ''){
    $start = true;
    $end = true;
}
$result = $mysqli->prepare("SELECT * 
                            FROM postdata 
                            where id = ? 
                              and title = ? 
                              and (date between ? and ?) 
                            order by date desc");

$result ->bind_param("isss", $id, $title, $start, $end);
$result->execute();

Actually i have a form with 3 fields. 1 title and others 2 are start and end for date.

If user either enter value in title or put start and end date in the form. User can also put values in both filed. Once data is submitted by user then i want to perform search in database.

Please advise why my query is not working.

12
  • Simple code indentation not only makes the code easier to read it also makes debugging much easier Commented Apr 11, 2016 at 19:49
  • = is assignment and == or === is an equality test. Commented Apr 11, 2016 at 19:51
  • title = '%%' says the content of the title column must contain %% and only %%. You are not using a LIKE and that is the only syntax that responds to % characters Commented Apr 11, 2016 at 19:55
  • true is not going to help you either Commented Apr 11, 2016 at 19:57
  • This is what i have at the moment. Commented Apr 11, 2016 at 19:57

2 Answers 2

1

You simply re-bind these variables in your IF statement

if($start = '' or $end = '')

There must be boolean operators like === or ==

Sign up to request clarification or add additional context in comments.

3 Comments

Although that whole IF is a bit of nonsense if you look at it even with the correct usage of ==
Applogies but = was typo. I already tried == but it is not returning any data.
Agree. The date() function won't return an empty string any way. The check should be before the transformation. However with '=' in IF statement it hasn't any chance :)
1

I created three separate select queries in if condition and it resolves the issue. Here is the code which i am using at the moment.

 if($title != '' and $_POST['start'] == '' and $_POST['end'] == ''){
      $result = $mysqli->prepare("SELECT * FROM posting where id = ? and title like ? order by date desc");
      $result ->bind_param("is", $id, $title);
 }
 if($title == '' and $_POST['start'] != '' and $_POST['end'] != ''){
      $result = $mysqli->prepare("SELECT * FROM posting where id = ? and (date between ? and ?) order by date desc");
      $result ->bind_param("iss", $id, $start, $end);
            }
 if($title != '' and $_POST['start'] != '' and $_POST['end'] != ''){
      $result = $mysqli->prepare("SELECT * FROM posting where id = ? and title like ? and (date between ? and ?) order by date desc");
      $result ->bind_param("isss", $id, $title, $start, $end);
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.