如何按用户指定的日期范围显示数据库中的数据,以及将用户输入值与数据库字段值进行比较。
答案 0 :(得分:2)
查询是这样的:
$sql = "SELECT * FROM table WHERE date between '".$date_min"' AND '".date_max"'";
假设$ date_min和$ date_max是您的用户输入的值,并将数据库表格列在您要查询的位置。
php将类似于:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM table WHERE date between '".$date_min"' AND '".date_max"'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
如果你为用户输入做了部分。
这些来自http://www.w3schools.com/php/php_mysql_select.asp。
在提出问题之前需要进行一些研究。
此致
答案 1 :(得分:1)
现在,你可以尝试这样的事情,使用ajax请求(需要jQuery):
JS:
/**
* Created by evochrome on 26-3-2016.
*/
$( document ).ready(function() {
var timer;
$('#reset').click(function(e) {
$('input').val(""); //set value equal to zero
});
$('input').on("input", function (e) {
clearInterval(timer);
timer = setTimeout(getTableContents,2000);//Execute function `getTableContents` after 3s
});
});
function getTableContents() {
$('td').fadeOut(500, function(){ $(this).parent().remove();});
setTimeout(function() {
var inputs = {};
$(".theader input").each(function () {
if ($(this).val() != '') {
inputs[$(this).attr('name')] = $(this).val();
}
});
$.ajax({ //ajax request
method: "POST",
url: "../php/fetch.php",
data: inputs,
dataType: 'json',
cache: false,
success: function (data) {
var tmpObject = {};
var i = 0;
data.forEach(function (row) {
var obj = data[i];
$('table').append("<tr id='t" + i + "'></tr>");
Object.keys(obj).forEach(function (key) {
$('#t' + i).append("<td style='display: none;'>" + row[key] + "</td>");
});
i++
});
$('td').fadeIn(500);
}
})
}, 500);
}
CSS:
table {
width: 80%;
}
td {
width: 40%;
background-color:green;
height: 40px;
text-align: center;
}
.theader{
width:100%;
justify-content:space-around;
display:flex;
}
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>T-Test</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Input Some Data</h1>
<div class="theader">
<input name="FromDate" type="date"/>
<input name="ToDate" type="date"/>
<button id="reset">Reset</button>
</div>
<table>
<tr>
<td>2</td>
<td>John</td>
<td>Appleseed</td>
</tr>
</table>
<script src="js/jquery-2.2.0.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
PHP:
<?php
/**
* Creator: evochrome
* Date: 26-3-2016
* Time: 15:40
*/
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', '1');
$conn = new mysqli('localhost', $db_name, $db_password, $db_user);
if (! $conn){
echo 'Oh damn a crash!';
die(' Connection failed :(');
}
$fields = array(
'FromDate', 'ToDate'
);
$where = "1=1"; //true
foreach ($fields as $field) {
if ($value = $_POST[$field]) {
$where .= " AND $field = '$value'"; //for each field add clause
}
}
$query = $conn->query("SELECT * FROM $table_name WHERE $where"); //insert tablename and where clause
$array = array();
while($row = $query->fetch_assoc()){ //fetch from db
$array[] = $row;
}
echo json_encode($array);
$query->close();
$conn->close();