【发布时间】:2017-07-23 09:54:27
【问题描述】:
我正在尝试根据用户输入的内容对数据库进行简单搜索,因为我正在使用 javascript 和 PHP。
它几乎可以工作,但我无法将用户正在输入的字符传递给 SQL 查询。
我确定该变量到达 Ajax_request.php 的 PHP POST 方法,但我不确定“functions.php”中的类是否已正确声明以使事情正常运行。
可能只是一个愚蠢的错误,但我对 PHP 和 Web 开发还很陌生
这些是我的文件:
index.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>InChiaro Ticket Admin</title>
<meta name="description" content="The HTML5 Herald" />
<meta name="author" content="SitePoint" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<div id="result"></div>
<input type="text" class="search-filter" id="searchcodiceCliente" name="codiceCliente" /> <!-- text AREA CODICE CLIENTE-->
<script type="text/javascript">
$(function () {
// We add the event on the class, which both inputs have
$(".search-filter").keyup(function () {
// Now we get the values from both inputs, using their ID's
var codiceCliente = $("#searchcodiceCliente").val();
//var fname = $("#searchfname").val();
// Add both to the dataString (and URI encode the strings)
var requestCodCliente = "get_codiceCliente_json"
// Check that at least one has any content
if (codiceCliente != '')
$.ajax({
type: "POST",
url: "ajax_requests.php",
data: 'request='+ requestCodCliente +'&searchCliente='+ codiceCliente,
success: function (result) {
console.log(result);
}
});
});
});
</script>
</body>
</html>
Ajax_request.php
<?php
if (!empty($_POST)) {
$codCliente = $_POST['searchCliente'];
$method = $_POST['request'];
include 'Database.php';
include 'functions.php';
$db = new Database();
$functions = new Functions($db, $codCliente);
if (method_exists($functions, $method)) {
$data = $functions->$method();
header('Content-Type: application/json');
echo json_encode($data);
}
}
?>
functions.php
<?php
/*function doLog($text)
{
// open log file
$filename = "log.txt";
$fh = fopen($filename, "a") or die("Could not open log file.");
fwrite($fh, date("d-m-Y, H:i")." - $text\n") or die("Could not write file!");
fclose($fh);
}
doLog($codCliente);*/
class Functions
{
private $db;
private $codCliente;
public function __construct(Database $db)
{
$this->db = $db;
}
public function setCodCliente($codCliente)
{
$this->codCliente = $codCliente;
}
public function get_codiceCliente_json()
{
$query = "SELECT * FROM clienti WHERE codiceCliente LIKE '%" . $this->codCliente . "%' LIMIT 15";
$result = $this->db->dataQuery($query);
return $result->fetchAll();
}
}
?>
还有另一个名为 Database.php 的文件只是与数据库建立连接。
感谢您的建议!
更新:
来自mysql的日志
2017-07-23T10:58:26.359942Z 65 Query SELECT * FROM clienti WHERE codiceCliente LIKE '%%' LIMIT 15
【问题讨论】:
-
什么时候出现错误?
-
我没有得到真正的错误,查询返回给我所有的数组,就像这样写的: $query = "SELECT * FROM clienti LIMIT 15";所以我认为问题是将变量 $this->codClienti 传递给查询字符串,或者类中的某些东西
-
更新:这是 mysql 关于特定查询的日志:2017-07-23T10:58:26.359942Z 65 Query SELECT * FROM clienti WHERE codiceCliente LIKE '%%' LIMIT 15
标签: javascript php sql search