问题描述
我使用AJAX来从PHP发送值PHP和检索值。问题是价值,我从PHP越来越被看作是未定义的AJAX。请帮我解决这个问题。
AJAX code:
VAR渠道;
功能全面(){
$(1)显示()。
$(二。)隐藏()。
$(三。)隐藏()。
$(四。)隐藏()。
窗口['通道'] =综合;
$阿贾克斯({
键入:GET,
网址:dash2.php
数据:({通道:通道}),
成功:功能(数据){
执行console.log(data.a);
执行console.log(data.b);
执行console.log(data.c);
}
});
}
PHP code:
< PHP
$通道= $ _ GET ['通道'];
$主机=192.168.0.29;
$用户名=根;
$密码=根;
$ DBNAME =realcl;
的mysql_connect($主机,$的用户名,密码,$)或死亡(无法连接到数据库,请稍后重试!);
mysql_select_db($数据库名);
$查询='选择*'$通道。
$ masterresult =请求mysql_query($查询);
而($ ROW1 = mysql_fetch_array($ masterresult))
{
$成功= $ ROW1 [1];
$超时= $ ROW1 [2];
$失败= $ ROW1 [3];
}
回声json_en code(阵列(A=>中$的成功,B=>中$超时,C=>中$失败));
?>
只是一个点没有其他人已经覆盖尚未:您需要用双引号或串联的位置:
'SELECT * FROM $频道; // 没有
选择从$频道*; // 是
选择*'$通道。 // 是
变量将不会被单引号内解决,这样你想从一个被称为字面上表,选择 $频道
。
另外,请使用某种验证对 $频道
为你所拥有的是非常容易受到SQL注入攻击。
I am using AJAX to send values to PHP and retrieve the values from PHP. The problem is the value i am getting from PHP is viewed as undefined in AJAX. Please help me solve this issue.
AJAX code:
var channel;
function overall() {
$(".one").show();
$(".two").hide();
$(".three").hide();
$(".four").hide();
window['channel'] = "OVERALL";
$.ajax({
type: "GET",
url: "dash2.php",
data: ({channel: channel}),
success: function (data) {
console.log(data.a);
console.log(data.b);
console.log(data.c);
}
});
}
PHP code:
<?php
$channel=$_GET['channel'];
$host="192.168.0.29";
$username="root";
$password="root";
$dbname="realcl";
mysql_connect($host,$username,$password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
$query = 'select * from '.$channel;
$masterresult = mysql_query($query);
while($row1 = mysql_fetch_array($masterresult))
{
$success=$row1[1];
$timeout=$row1[2];
$fail=$row1[3];
}
echo json_encode(array("a"=>"$success","b"=>"$timeout","c"=>"$fail"));
?>
Just a point no-one else has covered yet: you need to use double quotes or concatenation here:
'select * from $channel'; // no
"select * from $channel"; // yes
'select * from '.$channel; // yes
Variables won't be resolved inside single quotes so you are trying to select from a table that is literally called $channel
.
Also please use some kind of validation on $channel
as what you have is very vulnerable to SQL injection attack.
这篇关于AJAX显示检索到的值是不确定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!