Php 如何阻止此SQL查询的结果替换以前的结果?
我正在使用下面的HTML表单Php 如何阻止此SQL查询的结果替换以前的结果?,php,sql,Php,Sql,我正在使用下面的HTML表单 <form method="POST"> <input type="number" placeholder="first" name="first"> <input type="number" placeholder="last" name="last"> <input type="submit"> </form> 当值通过表单过帐时,这将成功返回介于第一个和最后一个之间的条目。但
<form method="POST">
<input type="number" placeholder="first" name="first">
<input type="number" placeholder="last" name="last">
<input type="submit">
</form>
当值通过表单过帐时,这将成功返回介于第一个和最后一个之间的条目。但是,当进行新查询时,任何Prioriy获取的结果都会消失。当通过表单发布新请求时,如何将新结果添加到旧结果列表的底部?让我向您展示一个非常基本的示例,说明您必须尝试的内容 我们有一个HTML页面,其中包含一个表单和一个div来存储结果。 在该文档中,我们还有一些Javascript代码来处理表单提交事件。 对于Ajax查询,我使用jQuery以惰性的方式完成了它。如果您不能在代码中使用该库,请告诉我 myPage.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
<form id="myForm"><!--Note there's no methods here-->
<input id="inputFirst" type="number" placeholder="first" name="first">
<input id="inputLast" type="number" placeholder="last" name="last">
<input type="submit">
</form>
</div>
<div id="divResults">
<!--Results will be displayed here-->
</div>
</body>
</html>
同一文档中的Javascript代码(myPage.html)
//在主浏览器中甚至不需要这些第一句话
var divResults=window.document.getElementById(“divResults”);
var myForm=window.document.getElementById(“myForm”);
var inputFirst=window.document.getElementById(“inputFirst”);
var inputLast=window.document.getElementById(“inputLast”);
//正在侦听提交按钮
myForm.addEventListener('submit',sendMyAmazingAjax);
函数sendmazingajax(e){
//避免采用直邮方式发送表格
e、 预防默认值();
$.ajax({
url:'myAmazingScript.php',//这些都是很好的自我解释
键入:“post”,
数据:{
first:inputFirst.value,//表单输入的内容
last:inputLast.value
},
成功:功能(数据){
//数据是您在PHP文档中“回显”的任何内容
//我们将其添加到divResults的当前内容(如果有)
divResults.innerHTML+=数据;
}
});
}
现在,一些PHP在另一个文档中(myAmazingScript.PHP)。
(您可以在同一个文档中执行此操作,但这有点棘手,根本没有必要)
我不知道您使用哪种方法对数据库进行查询。我选择prepared staments作为示例,因为它安全、简单,而且扩展性很强
<?php
// Creating connection
$mysqli = new mysqli('localhost', 'user', 'password', 'myAmazingDatabase');
// Checking connection
if (mysqli_connect_errno()) {
echo "Connect failed: %s\n", mysqli_connect_error();
exit();
}
$first = $_POST["first"];
$last = $_POST["last"];
$query = "SELECT * FROM posts ORDER BY TimeStamp DESC LIMIT ?, ?";
if ($stmt = $mysqli->prepare($query)) {
$stmt->bind_param('ii', $first, $last);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo 'First field: ' . $row['myAmazingFirstField'] . '<br>';
echo 'Second field: ' . $row['myAmazingSecondField'] . '<br>';
// Etc...
}
$stmt->free_result();
$stmt->close();
}
您是否在没有限制的情况下进行了尝试
问题在于,每次单击提交按钮时,您都是通过post发送表单,页面将重新加载。如果你想在同一个页面上添加不同查询的结果,你必须通过AJAX来完成。同意这两种方法。Ajax检索分页数据,这样页面就不会重新加载,或者忽略$first并始终将其设置为0。ajax是正确的方式,但它太宽泛了,无法在这里用SO来回答。回答得很好,非常感谢您的帮助。
<!--Loading jQuery for easy Ajax methods-->
<!--(You can download it to your server)-->
<script src="http://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
<script>
// These first sentences aren't even necessary in main browsers
var divResults = window.document.getElementById("divResults");
var myForm = window.document.getElementById("myForm");
var inputFirst = window.document.getElementById("inputFirst");
var inputLast = window.document.getElementById("inputLast");
// Listening for submit button
myForm.addEventListener('submit', sendMyAmazingAjax);
function sendMyAmazingAjax(e) {
// To avoid the form to be send by direct post method
e.preventDefault();
$.ajax({
url: 'myAmazingScript.php', // These are well self explained
type: 'post',
data: {
first: inputFirst.value, // Content of form inputs
last: inputLast.value
},
success: function (data) {
// data is anything you "echo" in the PHP document
// We add it to the current contents of divResults (if any)
divResults.innerHTML += data;
}
});
}
</script>
<?php
// Creating connection
$mysqli = new mysqli('localhost', 'user', 'password', 'myAmazingDatabase');
// Checking connection
if (mysqli_connect_errno()) {
echo "Connect failed: %s\n", mysqli_connect_error();
exit();
}
$first = $_POST["first"];
$last = $_POST["last"];
$query = "SELECT * FROM posts ORDER BY TimeStamp DESC LIMIT ?, ?";
if ($stmt = $mysqli->prepare($query)) {
$stmt->bind_param('ii', $first, $last);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo 'First field: ' . $row['myAmazingFirstField'] . '<br>';
echo 'Second field: ' . $row['myAmazingSecondField'] . '<br>';
// Etc...
}
$stmt->free_result();
$stmt->close();
}