基于日期检索数据的Ajax PHP查询
Ajax PHP query to retrieve data based on date
我正在尝试使用Ajax&PHP。选择选项值与数据库列名配对时可以正常工作,但当我将其更改为特定于日期时,它就不起作用了。请浏览下面的代码&建议所需的更改。
HTML:
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">July</option> // I doesn't work if when I change the value to "''2015-07-01'' AND ''2015-07-31''"
<option value="2">August</option> // I doesn't work if when I change the value to "''2015-08-01'' AND ''2015-08-31''"
<option value="3">September</option> // I doesn't work if when I change the value to "''2015-09-01'' AND ''2015-09-30''"
<option value="4">October</option> // I doesn't work if when I change the value to "''2015-10-01'' AND ''2015-10-31''"
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
Javascript
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getleads.php?q="+str,true);
xmlhttp.send();
}
}
PHP:
<?php
$q = intval($_GET['q']);
/*Comments Start, I even tried if else with the select values being only numeric
if($q==1)
{
$q == "''2015-07-01'' AND ''2015-07-31''";
}
elseif ($q==2)
{
$q == "''2015-08-01'' AND ''2015-08-31''";
}
elseif ($q==3)
{
$q == "''2015-09-01'' AND ''2015-09-30''";
}
} elseif ($q==4){
$q == "''2015-10-01'' AND ''2015-10-31''";
} else ($q== )
{
$q == "*";
}
Comments End*/
$con = mysqli_connect('localhost','user','password','database');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM table WHERE ID = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Name</th>
<th>Number</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['frm_name'] . "</td>";
echo "<td>" . $row['frm_mobile'] . "</td>";
echo "<td>" . $row['frm_email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
我甚至试着去掉日期后面的斜线。请帮忙!!
您不应该在sql查询中注入变量;现在,在服务器端对字符串进行硬编码并不重要,但如果您更改格式以接受表单中的值,则会将查询打开为sql注入。
你应该使用事先准备好的语句;那么你就不需要任何引号,如果你在未来更改任何内容,你就不会意外添加任何错误。
除此之外,您得到的sql是错误的,请查看当您取消注释字符串分配时会发生什么:
SELECT * FROM table WHERE ID = ''2015-07-01' AND '2015-07-31''
两个引号都是错误的,AND
语法也是错误的。这是假设table
不是您真正的表名。
你可能想要这样的东西:
SELECT * FROM your_table WHERE your_date_field BETWEEN ? AND ?
您可以将任何有效的格式化日期绑定到问号。
请注意,mysql还有一个MONTH()
函数,在这种特定情况下,它将使查询更加容易。
i have tried the same code there is issue in your select query please try this one.. and debug..
first import this table
CREATE TABLE IF NOT EXISTS `fromtable` (
`id` int(15) NOT NULL,
`frm_name` varchar(25) NOT NULL,
`frm_mobile` varchar(25) NOT NULL,
`frm_email` varchar(25) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `fromtable`
--
INSERT INTO `fromtable` (`id`, `frm_name`, `frm_mobile`, `frm_email`)
VALUES
(1, 'abc', '987654120', 'abc@gmail.com'),
(2, 'pqr', '985472160', 'pqr@gmail.com');
保持html页面为
getleads.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','dbname');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM fromtable WHERE id = '".$q."'";
//echo $sql;exit;
$result = mysqli_query($con,$sql);
$r = mysqli_query($con,$sql)
or die("Error: ".mysqli_error($con));
echo "<table>
<tr>
<th>Name</th>
<th>Number</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['frm_name'] . "</td>";
echo "<td>" . $row['frm_mobile'] . "</td>";
echo "<td>" . $row['frm_email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
相关文章:
- Ajax-如何获取数据
- JS验证ajax返回的html中的表单数据
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 使用ajax将数据从一个步骤发送到下一个步骤的3步表单
- node.js请求数据事件未在CORS ajax调用中触发
- React ajax数据集成
- 发送和接收数据?Ajax、Jquery和PHP
- 从返回的数据 ajax post 创建一个变量
- Laravel在成功后附加数据ajax
- Python - 从服务器获取数据 (ajax) - 'str' 对象不是可调用的错误
- 阻止执行数据 ajax 属性
- 表单中的文本框数据-AJAX提交时未传递
- 发送数据ajax php
- 加载更多数据ajax PHP onscroll
- Rails 4数据ajax路由问题
- 将数据ajax传递到每个页面的同一个页面
- 发送图像+数据ajax post到ashx处理程序
- 传递购物车数据…Ajax或PHP Post
- 将键值对作为单独的数据Ajax发布
- 使用数据Ajax发送