所以我有一個php文件,它打印JSON格式編碼表中的數據。發佈到php文件,並使用javascript檢索數據
這是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";
else
echo "error";
}
echo json_encode($data);
?>
這是JavaScript腳本:
$(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) {
if (data == "success") {
$.getJSON(url, function(result) {
console.log(result);
$.each(result, 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 if (data == "error") {
alert("error");
}
}
});
});
所以基本上IA有在那裏我有從數據庫中選擇所有項目(list_all.php),和那麼當我點擊單個項目時,該項目的ID就會傳遞到URL上,並且我會用javascript將其檢索到它。我不使用GET,因爲這是與phonegapp,所以我使用.js文件稱爲getURI.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)
服務器響應正確,因爲在應用其他腳本正在研究得到一個錯誤。
您的代碼是易受[** SQL注入**](HTTPS://en.wikipedia .org/wiki/SQL_injection)攻擊。你應該使用[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)或[** PDO **](https://secure.php.net/ manual/en/pdo.prepared-statements.php)準備帶有綁定參數的語句,如[**這篇文章**]所述(https://stackoverflow.com/questions/60174/how-can-i-prevent-sql步噴射功能於PHP)。 –
檢查Apache的錯誤日誌它說什麼? –
@AgamBanga我不認爲我有權訪問那個..但是一切正確與我的代碼?基本上,腳本獲取通過URL傳遞的id,發佈到PHP文件,php文件獲取ID,查詢關於該ID的數據庫,並返回json數據。然後$ .getJSOn函數將數據處理到變量中...... – Emerenciana