[英]PHP MYSQL query returns null with Ajax data, but not hardcoded data
I have the following PHP script: 我有以下PHP脚本:
include("dbconnecti.php");
$cropId = $_POST['cropId'];
//echo 'the id is: ' . $cropId;
$query = "SELECT W.*,FI.*, PN.*, CONCAT(FI.fName, ' ', FI.lname) AS farmer
FROM `wantToSell` AS W, `produceName` AS PN, `farmerInfo` AS FI
WHERE W.farmerId = FI.farmerId AND W.produceId = PN.produceId AND W.produceId = '" . $cropId ."'";
$result = $dbconnect->query($query);
if($result){
while($row = $result->fetch_assoc()){
$allRows[] = $row;
}
echo json_encode($allRows);
}
else{
echo json_encode($dbconnect -> error);
die;
}
}
And JQuery script: 和JQuery脚本:
function cropDescrip(clicked_id) {
$.ajax({
url : "../php/cropdescrip.php",
type : "POST",
dataType : 'JSON',
cache : false,
data : {cropId : clicked_id},
success : function (data) {
console.log(data);
} //end success
}); //end ajax
} //end cropDescrip
If I replace $_POST[cropId]
with a actual value (eg tmt001) the query statement returns a valid result. 如果我将
$_POST[cropId]
替换为实际值(例如tmt001),则查询语句将返回有效结果。 But when I pass a value to $_POST[cropId]
via a jQuery Ajax call, the SQL query returns an empty set. 但是,当我通过jQuery Ajax调用将值传递给
$_POST[cropId]
,SQL查询将返回一个空集。
The echo
statement shows that the value is being passed to the PHP script. echo
语句显示该值正在传递给PHP脚本。
What is happening, and how do I fix it? 发生了什么事,我该如何解决?
Perhaps it would be best to change your code to use a prepared statement. 也许最好更改代码以使用准备好的语句。 It might solve your problem as well as removing your sql injection risk.
它可能会解决您的问题以及消除SQL注入风险。 Something like this:
像这样:
$stmt = $mysqli->prepare("SELECT W.*,FI.*, PN.*,
CONCAT(FI.fName, ' ', FI.lname) AS farmer
FROM `wantToSell` AS W, `produceName` AS PN, `farmerInfo` AS FI
WHERE W.farmerId = FI.farmerId AND W.produceId = PN.produceId AND W.produceId =?";
/* assuming cropId is a string given your quotes in the original */
$stmt->bind_param("s", $cropId);
/* execute query */
$stmt->execute();
$result = $stmt->get_result();
Just guessing here, but it looks like you're passing the clicked_id
of the element, and not the value of that element. 只是在这里猜测,但是好像您正在传递元素的
clicked_id
,而不是该元素的值 。 Try this from your jQuery code: 从您的jQuery代码中尝试一下:
function cropDescrip(clicked_id) {
$.ajax({
url : "../php/cropdescrip.php",
type : "POST",
dataType : 'JSON',
cache : false,
data : {cropId : $("#" + clicked_id).val()}, // <-- HERE
success : function (data) {
console.log(data);
} //end success
}); //end ajax
} //end cropDescrip
This will find the element with the id, using element_id
as the selector value. 这将使用
element_id
作为选择器值来查找具有id的元素。 It will capture the value of that element, and pass it through the cropId
key to the PHP server. 它将捕获该元素的值,并将其通过
cropId
键传递给PHP服务器。
If this doesn't work as-is, it would be very useful to know precisely what clicked_id
actually is (show the HTML and Javascript code for it), and what value is being passed to the PHP server in $_POST[cropId]
. 如果这不能按原样工作,那么准确地了解一下
clicked_id
实际上是什么(显示其HTML和Javascript代码),以及在$_POST[cropId]
什么值传递给PHP服务器,将非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.