问题:我可以将值传递给ajax方法并成功实现Select SQL(在网络中看到的结果),但它永远不会返回任何结果(我的结果是警报("成功)。问题是什么它?
HTML:
function selectupdate_Server(id)
{
var ID= id;
console.log(ID); --->Successfully logged
$.ajax({
url: "abc.php",
type: 'POST',
data: {"ID": ID},
dataType: 'json',
success: function (data) {
alert ('Success'); ------>Failed to alert
}
});
}
PHP:
$serverName = "localhost";
$username = "root";
$password = "root";
$dbName = "noticeboard";
$tbName1 = "notice";
$conn = new mysqli($serverName, $username, $password, $dbName);
checkStaffLogin();
function checkStaffLogin()
{
global $conn, $tbName1;
if ($conn->connect_errno)
{
echo "Fail to connect MYSQL database : " . $conn->connect_errno;
}
else {
$ID= $_POST["ID"];
$sql = "SELECT *
FROM $tbName1 where ID= '$ID'";
$result = $conn->query($sql);
if($result)
{
while($row =mysqli_fetch_assoc($result) )
{
$table_data[]= array("Name"=>$row['Name'],"Password"=>$row['Password']);
}
echo json_encode($table_data);
}
答案 0 :(得分:0)
尝试此查询
$table_data=[];
$sql = "SELECT * FROM ".$tbName1." where ID = ".$ID;
注意:使用preperd语句
并将此标题附加到您的php header('Content-Type: application/json');
循环结果使用
success: function (data) {
$.each(data,function(i,v){
console.log(v.Name)
});
}
注意数据应如下所示:
data: {ID: ID},
答案 1 :(得分:0)
<?php
$ID= $_POST["ID"];
$sql = "SELECT * FROM ".$tbName1."where ID=".$ID;
$result = $conn->query($sql);
while($row =mysqli_fetch_assoc($result) )
{
$table_data[]= array("Name"=>$row['Name'],"Password"=>$row['Password']);
}
echo json_encode($table_data);
?>
试试这个
答案 2 :(得分:0)
在JS中,通过删除那些&#34; ID&#34;来检查数据的语法。请尝试以下
function selectupdate_Server(id)
{
var ID= id;
$.ajax({
url: "abc.php",
type: 'POST',
data: {ID: ID},
dataType: 'json',
success: function (data) {
alert ('Success');
}
});
}
答案 3 :(得分:0)
你试试这个Javascript函数
function selectupdate_Server(id)
{
var ID= id;
console.log(ID); --->Successfully logged
$.ajax({
type: 'POST',
data: {ID: ID},
url: "abc.php",
dataType: 'json',
success: function(resultss){
try{
alert ('Success');
}catch(e)
{
alert(JSON.stringify(e)+"Catch error");//any Error in Your php file display alert.where,what error
}
},
error: function(e){
alert(JSON.stringify(e)+"error");//any Error in Your php file display alert.where,what error
}
});
}
这个脚本,你的php文件中的错误提醒你行no和你检查和更正的文件名
和你这样的Php代码
checkStaffLogin();
function checkStaffLogin()
{
$serverName = "localhost";
$username = "root";
$password = "root";
$dbName = "noticeboard";
$tbName1 = "notice";
$conn = mysqli_connect($serverName, $username, $password, $dbName);
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
$ID= $_POST["ID"];
$table_data=array();
$sql = "SELECT * FROM $tbName1 where ID= '$ID'";
$result = mysqli_query($conn,$sql) or die(mysql_error());
if($result)
{
while($row =mysqli_fetch_assoc($result) )
{
$table_data[]= array("Name"=>$row['Name'],"Password"=>$row['Password']);
}
echo json_encode($table_data);
}
}
}
你试试这个