php - Passing selected date value to a query -
this question has answer here:
i found similar questions here of solutions don't work me. have form 'jquery ui date picker' , want search results on given date. tried changing date formats doesn't return result.
search form-
<div class="form-group row"> <label for="duration" class="col-sm-4 col-form-label"> <input type="checkbox" name="search2" value="checkbox" id="duration" class="search2"> duration</label> <div class="col-sm-4"> <input type="date" class="form-control input-sm data2" name="data1" id="startdate" placeholder="start date" disabled="disabled"> </div> <div class="col-sm-4"> <input type="date" class="form-control input-sm data2" name="data2" id="enddate" placeholder="end date" disabled="disabled"> </div> </div>
search query-
$sql = "select * reservation inner join package on reservation.tid = package.tid inner join reservation_type on reservation.type = reservation_type.id inner join customer on reservation.cid = customer.cid inner join reservation_status on reservation.status = reservation_status.id " ; if(isset($_post['search2'])){ $start_date = date('y-m-d', strtotime($_post['data1'])); $sql .= " start_date = $start_date and" ; } if(isset($_post['search2'])){ $end_date = date('y-m-d', strtotime($_post['data2'])); $sql .= " end_date = $end_date and" ; } if(isset($_post['search'])){ $rid = mysqli_real_escape_string($db, $_post['data']); $sql .= " rid = $rid and" ; } if(isset($_post['search3'])){ $cid = mysqli_real_escape_string($db, $_post['data3']); $sql .= " reservation.cid = $cid and" ; } if(isset($_post['search5'])){ $tid = mysqli_real_escape_string($db, $_post['data5']); $sql .= " reservation.tid = $tid and" ; } if(isset($_post['search6'])){ $type = mysqli_real_escape_string($db, $_post['data6']); $sql .= " reservation.type = $type and" ; } if(isset($_post['search4'])){ $status = mysqli_real_escape_string($db, $_post['data4']); $sql .= " status = $status and" ; } $sql = rtrim($sql, "and") ; $result = mysqli_query($db, $sql)or die("error: ".mysqli_error($db)); $num_rows = mysqli_num_rows($result); if($num_rows >0){ while ($row = mysqli_fetch_assoc($result)) { ?> <tr align="center"> <td><?php echo $row["start_date"]; ?></td> <td><?php echo $row["end_date"]; ?></td>
checkbox selection-
$(".search2").on('click', function() { if($(this).is(':checked')){ $(".data2").prop('disabled',false); } else { $(".data2").prop('disabled',true); } })
jqueryui-
$( function() { var dateformat = "yy-mm-dd", startdate = $( "#startdate" ) .datepicker({defaultdate: "+1w",changemonth: true, numberofmonths: 1}) .on( "change", function() {enddate.datepicker( "option", "mindate", getdate( ) );}), enddate = $( "#enddate" ) .datepicker({defaultdate: "+1w",changemonth: true,numberofmonths: 1}) .on( "change", function() {startdate.datepicker( "option", "maxdate", getdate( ) );}); function getdate( element ) { var date; try { date = $.datepicker.parsedate( dateformat, element.value ); } catch( error ) { date = null; } return date; } } ); $('#startdate').datepicker({dateformat: 'yy-mm-dd'}); $('#enddate').datepicker({dateformat: 'yy-mm-dd'});
query works fine other ckeckings (as image below); search result 1
but not date, search result 2
i student , new coding. appreciated! thank you.
in query, put single quotes values : eg :
$sql .= " start_date = '$start_date' and" ;
Comments
Post a Comment