我尝试存储使用javascript获取的浏览器地理位置,并通过$.ajax
通过php发布到mysql表中。我收到发布的数据并以递归数组转换,因此我只能获得纬度和经度数据,但我得到两个警告(我将对代码进行评论):
1->Warning: mysqli_real_escape_string() expects parameter 2 to be string, object given in.
2->Warning: mysqli_error() expects exactly 1 parameter, 0 given in
这是我的代码:
地理定位并发送数据:
if (window.navigator.geolocation) {
var failure, success;
success = function (position) {
console.log(position);
var stringData = JSON.stringify(position, null);
$.ajax({
type: "POST",
url: "GL_.php",
data: {
data: stringData
}
});
};
failure = function (message) {
alert('Cannot retrieve location!');
};
navigator.geolocation.getCurrentPosition(success, failure, {
maximumAge: Infinity,
timeout: 5000
});
}
...接收数据 - > ...
<? php
$hostname_connection = "p:localhost";
$database_connection = "s_c"
$username_connection = "root";$password_connection = "";
$cs_connection = mysqli_connect($hostname_connection, $username_connection, $password_connection, $database_connection) or trigger_error(mysqli_error(), E_USER_ERROR); mysqli_set_charset($cs_connection, 'utf8');
function mysqli_result($res, $row, $field = 0) {
$res - > data_seek($row);
$datarow = $res - > fetch_array();
return $datarow[$field];
}
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
global $cs_connection;
$theValue = > function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($cs_connection, $theValue) : mysqli_escape_string($theValue); //FIRST WARNING
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'".$theValue."'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'".$theValue."'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
if (isset($_POST['data'])) {
$dataString = $_POST['data'];
}
function geoCodeUser($dataString) {
global $database_connection;
global $cs_connection;
$position = json_decode($dataString, true);
$lat = $position['coords']['latitude'];
$lng = $position['coords']['longitude'];
if ($dataString !== NULL) {
$insertLatLng = sprintf("INSERT INTO usergeoloc (lat,long) VALUES (%s, %s)", GetSQLValueString($cs_connection, $lat, "text"), GetSQLValueString($cs_connection, $lng, "text"));
$Result1 = mysqli_query($cs_connection, $insertLatLng) or die(mysqli_error($cs_connection)); //SECOND WARNING
} else {
echo "NO CONTENT";
}
}
geoCodeUser($dataString);
?>
变量$ lat和$ lng每个都填充相应的值,但正如我之前提到的,错误出现了。任何人都可以解释这里的错误吗?
答案 0 :(得分:1)
对于第一个错误,您的问题是您调用GetSQLValueString方法错误,您将其定义为
GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
但是用这些参数调用它
GetSQLValueString($cs_connection, $lat, "text"),
GetSQLValueString($cs_connection, $lng, "text")
所以$ theValue被设置为一个对象(mysqli链接)
至于mysqli_error错误,你没有传递必需的参数
http://php.net/manual/en/mysqli.error.php
你有:程序风格
字符串mysqli_error(mysqli $ link)
die(mysqli_error())
应该是
die(mysqli_error($cs_connection))
答案 1 :(得分:0)
抓住这样:
if($cs_connection){
$theValue = mysqli_real_escape_string($cs_connection, $theValue);
} else {
// No DB Connection, so no way reason to escape
}