Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法使用PHP在数据库中保存数据_Javascript_Php_Mysql_Mysqli - Fatal编程技术网

Javascript 无法使用PHP在数据库中保存数据

Javascript 无法使用PHP在数据库中保存数据,javascript,php,mysql,mysqli,Javascript,Php,Mysql,Mysqli,我使用以下代码在PHP中保存数据。我要保存的数据是: Latitudes:"[47.99267886541119,47.81223227508317]" Longitudes:"[19.403228759765625,19.015960693359375]" 请帮我纠正代码中的任何错误 <?php $con = mysqli_connect('localhost','root',''); if(!$con) { echo 'Not Connected To Server'; }

我使用以下代码在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>

每次循环迭代时,都将重置$Latitude和$Longitude值。你应使用:

$Latitude .= $_POST['Latitudes'][$i]; // And some logic to add comma till last value is reached.
问题 POST值,即$_POST[‘经度’]和$_POST[‘经度’]是字符串,而不是数组

请参阅中的证明

解决 有多种方法可以处理此问题-一些示例包括:

使用2个或更多文本输入,即带有支持数组语法的属性的标记,有关更多信息,请参阅:

<input name="Latitudes[]" type="text" id="lat1" size="100" value='' readonly/>
<input name="Latitudes[]" type="text" id="lat2" size="100" value='' readonly/>
解析这些字符串(例如,使用正则表达式)可能会很麻烦
首先发布错误,html可能比回显您自己的失败字符串更有用。html代码也已粘贴。消息仍然没有插入@Fred ii注:mysqli的面向对象接口明显不那么冗长,代码更易于阅读和审核,并且不容易与过时的mysql_查询接口混淆。在你对程序性风格投入太多之前,值得换一种。示例:$db=new mysqli…和$db->prepare…过程接口是引入mysqli API时PHP4时代的产物,不应在新代码中使用。警告:使用mysqli时,应使用and将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。永远不要将$\u POST、$\u GET或任何用户数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。