使用两个datetimepicker(mysql,PDO和AJAX)检索数据 [英] Retrieving data using two datetimepicker (mysql, PDO and AJAX)

查看:87
本文介绍了使用两个datetimepicker(mysql,PDO和AJAX)检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我正在尝试使用两个datetimepicker从数据库中检索数据,但是当将date date rage设置为datetimepicker时,我单击按钮会显示所有数据。以下是我的整个代码。请帮我。非常感谢你。



我尝试过:



index.php



Hi! I'm trying to retrieve data from database using two datetimepicker but when set date rage to the datetimepicker and I click button all the data are displaying. Below are my whole codes. Please help me. Thank you so much in advance.

What I have tried:

index.php

<tbody id = "load_data">
						<?php

							$db = new PDO('mysql:host=localhost;dbname=db_search;charset=utf8mb4', 'root', '');
    						$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    						$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
							
							$sql = "select * from book ORDER BY date_published ASC";
							$stmt = $db->prepare($sql);
							$stmt->execute();
							//while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
							foreach($stmt as $row) {
						?>
						<tr>
							<td><?php echo $row['ISBN']?></td>
							<td><?php echo $row['title']?></td>
							<td><?php echo $row['author']?></td>
							<td><?php echo date("m/d/Y", strtotime($row['date_published']))?></td>
						</tr>
						<?php
							}
						?>
					</tbody>





get_data.php





get_data.php

<?php
$date1 = date("Y-m-d", strtotime($_POST['date1']));
$date2 = date("Y-m-d", strtotime($_POST['date2']));

$db = new PDO('mysql:host=localhost;dbname=db_search;charset=utf8mb4', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
	
$sql = "select * from book where date_published";
$stmt = $db->prepare($sql);
$stmt->execute(); 
$row_count = $stmt->rowCount();
if($row_count > 0){
	foreach($stmt as $row) {
	?>
	<tr>
		<td><?php echo $row['ISBN']?></td>
		<td><?php echo $row['title']?></td>
		<td><?php echo $row['author']?></td>
		<td><?php echo date("m/d/Y", strtotime($row['date_published']))?></td>
	</tr>
	<?php
	}
}else{
		echo '
		<tr>
			<td colspan = "4"><center>Record Not Found</center></td>
		</tr>
		';
}
	?>





ajax.js





ajax.js

$(document).ready(function(){
	$('#date1').datepicker();
	$('#date2').datepicker();
	$('#btn_search').on('click', function(){	
		if($('#date1').val() == "" || $('#date2').val() == ""){
			alert("Please enter something on the text field");
		}else{
			$date1 = $('#date1').val();
			$date2 = $('#date2').val();
			$('#load_data').empty();
			$loader = $('<tr ><td colspan = "4"><center>Searching....</center></td></tr>');
			$loader.appendTo('#load_data');
			setTimeout(function(){
				$loader.remove();
				$.ajax({
					url: 'get_data.php',
					type: 'POST',
					data: {
						date1: $date1,
						date2: $date2
					},
					success: function(res){
						$('#load_data').html(res);
					}
				});
			}, 3000);
		}	
	});
	
	$('#reset').on('click', function(){
		location.reload();
	});
});

推荐答案

db = new PDO('mysql:host = localhost; dbname = db_search; charset = utf8mb4 ','root','');
db = new PDO('mysql:host=localhost;dbname=db_search;charset=utf8mb4', 'root', '');


db-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


db - > setAttribute(PDO :: ATTR_EMULATE_PREPARES,false);
db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


这篇关于使用两个datetimepicker(mysql,PDO和AJAX)检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆