【发布时间】:2017-11-07 18:47:31
【问题描述】:
我正在使用以下代码在 PHP 中保存数据。我要保存的数据是:
Latitudes:"[47.99267886541119,47.81223227508317]"
Longitudes:"[19.403228759765625,19.015960693359375]"
请帮我纠正代码中的任何错误。
<?php
$con = mysqli_connect('localhost','root','');
if(!$con)
{
echo 'Not Connected To Server';
}
if (!mysqli_select_db ($con,'test'))
{
echo 'Database Not Selected';
}
for ($i=0;$i<count($_POST['Latitudes']);$i=$i+1)
{
$Latitude = $_POST['Latitudes'][$i];
}
for ($i=0;$i<count($_POST['Longitudes']);$i=$i+1)
{
$Longitude= $_POST['Longitudes'][$i];
}
$sql = "insert into polyline (lat,lng) values
('$Latitude','$Longitude')";
if (!mysqli_query($con,$sql))
{
echo 'Not Inserted';
}
else
{
echo 'Inserted Successfully';
}
header("refresh:100; url=Final edited copy.html");
?>
这是 HTML 代码:
var polyOptions = {
geodesic: true,
strokeOpacity: 1.0,
strokeWeight: 2,
}
var poly=new google.maps.Polyline(polyOptions);
poly.setMap(map);
var evtListnr = google.maps.event.addListener(map, "click", function(event){
var path = poly.getPath();
if (poly.getPath().getLength() == 1) {
google.maps.event.removeListener(evtListnr);
}
path.push(event.latLng);
var coordinates_poly=poly.getPath().getArray();
var lat_poly = [];
var lng_poly = [];
for(var i = 0; i <coordinates_poly.length; i++){
lat_poly.push(coordinates_poly[i].lat());
lng_poly.push(coordinates_poly[i].lng());
}
var str_lat_poly=JSON.stringify(lat_poly);
var str_lng_poly=JSON.stringify(lng_poly);
document.getElementById("data1").value= 'Latitudes:"'+str_lat_poly+'"';
document.getElementById("data2").value= 'Longitudes:"'+str_lng_poly+'"';
});
}
</script>
<form action="insert.php" method="POST">
<br/><input name="Latitudes" type="text" id="data1" size="100" value='' readonly/><br/>
<br/><input name="Longitudes" type="text" id="data2" size="100" value='' readonly/><br/>
<input type="submit">
</form>
【问题讨论】:
-
先发布错误,然后发布 html
-
mysqli's error reporting functions 可能比回显您自己的失败字符串更有用。
-
html 代码也已粘贴。消息仍然是“未插入”。 @Fred-ii-
-
注意:
mysqli的面向对象接口明显不那么冗长,使代码更易于阅读和审核,并且不容易与过时的mysql_query接口混淆。在您对程序风格投入过多之前,值得转换一下。示例:$db = new mysqli(…)和$db->prepare("…")程序接口是 PHP 4 时代引入的mysqliAPI 的产物,不应在新代码中使用。 -
警告:当使用
mysqli时,您应该使用parameterized queries 和bind_param将用户数据添加到您的查询中。 请勿使用字符串插值或连接来完成此操作,因为您创建了严重的SQL injection bug。 切勿将$_POST、$_GET或任何用户数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。
标签: javascript php mysql mysqli