发布到php文件,并使用javascript检索数据
我有一个php文件,它从一个以JSON格式编码的表中打印数据 这是php文件:发布到php文件,并使用javascript检索数据,javascript,php,ajax,phonegap,Javascript,Php,Ajax,Phonegap,我有一个php文件,它从一个以JSON格式编码的表中打印数据 这是php文件: <?php include "db.php"; $id=$_POST['id']; $data=array(); $q=mysqli_query($con,"select * from `sitios` where `id_sitio`='$id'"); while ($row=mysqli_fetch_object($q)){ $data[]=$row; } if($q) echo "success
<?php
include "db.php";
$id=$_POST['id'];
$data=array();
$q=mysqli_query($con,"select * from `sitios` where `id_sitio`='$id'");
while ($row=mysqli_fetch_object($q)){
$data[]=$row;
}
if($q)
echo "success";
else
echo "error";
}
echo json_encode($data);
?>
所以基本上我有一个数据库中所有项目的选择(list_all.php),然后当我点击一个项目时,该项目的ID被传递到URL上,我用javascript在另一端检索它。我不使用GET,因为这是phonegapp,所以我使用一个名为getURI.js的.js文件
首先,函数获取传递的ID。然后它发布到PHP文件,PHP文件将获得ID,并对数据库中的单个项进行查询。成功后,我想将所有数据存储在变量上。但是由于某些原因,我在控制台上得到一个错误
POST http://192.168.1.241:3000/proxy/http%3A%2F%2Fpedrofidalgo.pt%2Fbilapoint%2Flistar_sitio_single.php 500 (Internal Server Error)
服务器响应正确,因为应用程序上的其他脚本正在工作
在PHP中
您的代码易受攻击。您应该使用或准备带有绑定参数的语句,如中所述。检查apache错误日志它说了什么?@AgamBanga我想我没有访问权限。。但我的代码一切正常吗?基本上,脚本获取URL上传递的id,发布到PHP文件,PHP文件获取id,查询数据库中的id,并返回json数据。然后$.getJSOn函数将数据处理成变量…您是否可以打印
echo(“错误描述:”.mysqli_Error($con))代码>在$q=mysqli\u查询($con,“选择*from
sitio`whereid\u sitio
='id'”)之后`@AgamBanga它没有回显任何内容,因为php文件从未“打开”,它只是被另一个页面上的javascript函数调用……由此我得到了http 500错误。如果我从php文件中删除“If($q)echo json_encode(array('status'=>true,'data'=>$data));否则echo json_encode(array('status'=>false,'data'=>$data));}”,我不会得到错误更新的答案。If($q)echo“success”;否则回显“错误”;}代码>这也困扰着我。我以为这是另一个括号。JS也发生了变化还没有,我不认为“var id=decodeURI(getUrlVars()[“id”]);”正在被传递。。你能把我添加到skype上吗?是的,分享你的SkypedRofidalgorms
POST http://192.168.1.241:3000/proxy/http%3A%2F%2Fpedrofidalgo.pt%2Fbilapoint%2Flistar_sitio_single.php 500 (Internal Server Error)
<?php
include "db.php";
$id=$_POST['id'];
$data=array();
$q=mysqli_query($con,"select * from `sitios` where `id_sitio`='$id'");
while ($row=mysqli_fetch_object($q)){
$data[]=$row;
}
if($q)
echo json_encode(array('status' => true, 'data' => $data));
else
echo json_encode(array('status' => false, 'data' => $data));
?>
$(document).ready(function() {
var id = decodeURI(getUrlVars()["id"]);
var dataString = "id=" + id;
$.ajax({
type: "POST",
url: "http://pedrofidalgo.pt/bilapoint/listar_sitio_single.php",
data: dataString,
crossDomain: true,
cache: false,
success: function(data) {
data = JSON.parse(data);
if (data['status']) {
$.each(data['data'], function(i, field) {
var id = field.id_sitio;
var nome = field.nome;
var descricao = field.descricao;
var img = field.img;
var morada = field.morada;
var coordenada_x = field.coordenada_x;
var coordenada_y = field.coordenada_y;
document.getElementById("titulo").innerHTML = nome;
document.getElementById("desc").innerHTML = descricao;
document.getElementById("morada").innerHTML = morada;
});
} else {
alert("error");
}
}
});
});