我正在尝试根据用户输入的内容对数据库进行简单搜索,因为我正在使用,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
答案 0 :(得分:0)
我注意到的一些事情可能会导致您的问题......
1。由于日志中的部分SQL看起来像... LIKE '%%' ...
,这意味着您的属性codCliente未在您的实例中正确设置。
修改此行代码......
$functions = new Functions($db, $codCliente);
对此...
$functions = new Functions($db);
$functions->setCodCliente($codCliente);
您的构造函数没有为codCliente属性定义参数...
2。还要检查ajax调用中的 data 属性 它现在看起来像......
// ...
data: 'request='+ requestCodCliente +'&searchCliente='+ codiceCliente,
// ...
数据字段可以接受数组/对象作为值,因此如果您想确保正确发送和接收参数,请将其更改为类似的内容......
// ...
data: {
request: requestCodCliente,
searchCliente: codiceCliente
},
// ...
我希望这会有所帮助:)
答案 1 :(得分:0)
我认为问题是当你调用它时,你没有在$codCliente
构造函数中分配Functions
参数,
$functions = new Functions($db, $codCliente);
您应该像这样更新Functions
课程:
<?php
class Functions
{
private $db;
private $codCliente;
public function __construct(Database $db, $codCliente)
{
$this->db = $db;
$this->codCliente = $codCliente;
}
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();
}
}
这一次,当您调用get_codiceCliente_json
方法时,$this->codCliente
属性不再为空;)