user3386406
user3386406

Reputation: 99

PHP Datepicker in SQL query

so I have this page that shows today's matches by default, it has this datepicker form

<form method="post">
<p> Select Date:<input id="datepicker" type="text" size="8" /> </p>
<input type="submit" value="Submit" name="usub" />
</form>
<?php
    if(isset($_POST["usub"])){ $date = $_POST["datepicker"]; }
else{ $date = date('Y-m-d'); } 
$data = mysql_query("SELECT * FROM todaysmatches where matchdate='$date'") or die(mysql_error());

echo $_POST["usub"];
echo "<h4>$today Matches</h4> </br>";

//table
if (mysql_num_rows($data)==0){
     echo " No Matches";
     echo "</br>";
     echo "<h4> Sorry About That Check For other Days Or you can Check the Library</h4>";
 }
else{

echo "<table border='1'>
<tr>
<th>Match</th>
<th>Tourmanet</th>
<th>Date</th>
</tr>";

while($info = mysql_fetch_array( $data ))
  {
  echo "<tr>";
  echo "<td>" . $info['curmatch'] . "</td>";
  echo "<td>" . $info['tournamentname'] . "</td>";
  echo "<td>" . $info['matchdate'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
}
?>

what I want is if the user choose a date in the form it would go in the query and bring the data associated with that date while keeping today as the default one when they first load the page

Upvotes: 0

Views: 2785

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44864

You need to quote the datevalue in the query as

SELECT * FROM todaysmatches where matchdate= '$today'


$data = mysql_query("SELECT * FROM todaysmatches where matchdate='$today'") 

Now to get the datepicker value you need to change a bit in the form and use name attributes as

<form method="post">
<p> Select Date:<input id="datepicker" type="text" size="8" name="datepicker"/> </p>
<input type="submit" value="Submit" name="usub"/>
</form>

Then in PHP you have do as

if(isset($_POST["usub"])){
 $date = $_POST["datepicker"];
}

And use it in the query and in the else part you can have the query to get the data from today's date

NOTE : Make sure that the date you are passing from date picker to query is in proper format.

Upvotes: 4

Related Questions