wrichards0
wrichards0

Reputation: 187

PHP: Can't seem to query on a date with MySQL

For a system I am creating, I am trying to produce a search page. It has the following HTML:

 <?php include 'includes/header.php';
 ?>
 <fieldset>
 <legend>Edit all Entries</legend>
 <form action="editall.php" method="POST">
 <p><label for="mhost">Meeting Host</label><input type="text" name="mhost" id="mhost"></p>
 <p><label for="vname">Visitor Name</label><input type="text" name="vname" id="vname"></p>
 <p><label for="datepicker">Meeting Date</label><input type="text" name="datepicker" id="datepicker"></p>
 <p><input type="submit" name="submit"></p>
 </form>
 </fieldset>
 <?php if(isset($_POST["submit"])==TRUE)
 {
      $datepicker = trim($_POST["datepicker"]);
      if(empty($datepicker)==FALSE)
      {
           $datepicker = explode("/", $datepicker);
           $datepicker = "{$datepicker[2]}-{$datepicker[1]}-{$datepicker[0]}";
      }
      $mhost = htmlentities(trim($_POST["mhost"]));
      $vname = htmlentities(trim($_POST["vname"]));     
      $db_search = search_database($mhost, $vname, $datepicker, $connection);
      if($db_search==FALSE)
      {
           echo "<div class=\"centered_text\">Nothing found matching search</div>\n";
      }
      else
      {
           echo "<table>\n
           <thead>\n
           <tr>\n
           <th>Date</th>\n
           <th>Time</th>\n
           <th>Visitor(s)</th>\n
           <th>Company</th>\n
           <th>Host</th>\n
           <th>Room</th>\n
           <th></th>\n
           </tr>\n
           </thead>\n
           <tbody>\n";
           /* This will hold the number of meetings */
           foreach($db_search as $meeting)
           {
                $visitor_names = stripslashes($meeting["visitor_names"]);
                $company_name = stripslashes($meeting["company_name"]);
                $meeting_host = stripslashes($meeting["meeting_host"]);
                $meeting_id = (int)$meeting["meeting_id"];
                $start = date("g:ia", strtotime($meeting["meeting_start"]));
                $date = date("d/m/y", strtotime($meeting["meeting_start"]));
                $meeting_location = stripslashes($meeting["meeting_location"]);
                echo "<tr>\n";
                echo "<td>{$date}</td>\n";
                echo "<td>{$start}</td>\n";
                echo "<td>{$visitor_names}</td>\n";
                echo "<td>{$company_name}</td>\n";
                echo "<td>{$meeting_host}</td>\n";
                echo "<td>{$meeting_location}</td>\n";
                echo "<td><a href=\"edit.php?id={$meeting_id}\">Edit</a></td>\n";
                echo "</tr>\n";
           }     
           echo "</tbody>\n";
           echo "</table>\n";
      }
 }
 include 'includes/footer.php';
 ?>

My function that queries the database is as follows:

 function search_database($mhost, $vname, $datepicker, $connection)
 {
      $mhost = $connection->real_escape_string($mhost);
      $mhost = $connection->real_escape_string($vname);
      $query_search = $connection->query("SELECT `meeting_id`, `visitor_names`, `meeting_host`, `email_address`, `company_name`, DATE_FORMAT(`meeting_start`, '%Y-%m-%d') AS `meeting_start`, `meeting_location`, `host_extension` FROM `details` WHERE (`meeting_host` LIKE '%{$mhost}%' OR `visitor_names` LIKE '%{$vname}%' OR `meeting_start` = '{$datepicker}') AND (`visibility` = 0) ORDER BY `meeting_start` ASC");
      if($connection->error)
      {
           return $connection->error;
      }
      if($query_search->num_rows > 0)
      {
           $results = array();
           while($rows = $query_search->fetch_assoc())
           {
                $results[] = $rows;
           }
           return $results;
      }
      else
      {
           return FALSE;
      }
 }

The problem is that, while it works if you put in a host name or a visitor name, it doesn't work properly if I put a date in. When a date is put in, it will return all dates. I have no idea what the problem is, can you please help

Upvotes: 1

Views: 56

Answers (1)

Brian C
Brian C

Reputation: 134

Can't add a comment looking for clarification (low rep), but MySQL's default date format is YYYY-MM-DD, it's not 100% clear from your post that the date you are formatting in the correct format. If you are using US dates your MM/DD/YYYY would be converted to YYYY-DD-MM which would be invalid.

$datepicker = "{$datepicker[2]}-{$datepicker[1]}-{$datepicker[0]}";

So, if you are using US dates, you would want to use:

$datepicker = "{$datepicker[2]}-{$datepicker[0]}-{$datepicker[1]}";

Upvotes: 2

Related Questions