我正在使用Google Maps API制作路线。当我定义一个路由时,该值进入数据库但返回一个空值。数据库中的主键递增,但其他值为空。有什么想法吗?
<?php
session_start();
// connection to mysql
$latitude=$_POST['Lat'];
$longitude=$_POST['Long'];
$start_point=$_POST['s_point'];
$end_point=$_POST['e_point'];
$link = mysqli_connect("localhost","root","123456","db_mmw") or die ("Error" .mysqli_errno($link));
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO tbl_route (latitude,longitude,start_point,end_point) VALUES ('".$latitude."','".$longitude."','".$start_point."','".$end_point."')";
if (!mysqli_query($link,$sql))
{die('Error: ' . mysqli_error($link));
}
mysqli_close($link); ?>
答案 0 :(得分:2)
您在(现已删除的)评论中发布的内容是,您的表单包含name="long"
和name="lat"
那些与你的不匹配:
$latitude=$_POST['Lat']; // uppercase L
$longitude=$_POST['Long']; // uppercase L
$start_point=$_POST['s_point'];
$end_point=$_POST['e_point'];
将其更改为:
$latitude=$_POST['lat']; // lowercase l
$longitude=$_POST['long']; // lowercase l
$start_point=$_POST['s_point'];
$end_point=$_POST['e_point'];
旁注: 您现在的代码向SQL injection开放。使用prepared statements或PDO
修改/重写:强>
<?php
session_start();
$link = mysqli_connect("localhost","root","123456","db_mmw") or die ("Error" .mysqli_errno($link));
// connection to mysql
$latitude=$_POST['lat'];
$longitude=$_POST['long'];
$start_point=$_POST['s_point'];
$end_point=$_POST['e_point'];
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO tbl_route (latitude,longitude,start_point,end_point) VALUES ('".$latitude."','".$longitude."','".$start_point."','".$end_point."')";
if (!mysqli_query($link,$sql))
{
die('Error: ' . mysqli_error($link));
}
mysqli_close($link);
?>
<input type="submit" name="Define Route" value="Define Route"
在您的表单之外,会阻止它工作,加上缺少>
,应该读作:
<input type="submit" name="Define_Route" value="Define Route">
为了以防万一,我将其name="Define Route"
更改为name="Define_Route"
。
<div class ="form">
<form method="post" action=""><br> <table border="0">
<tr> <td>Start Point</td><td>
<input type="text" name="s_point"></td> </tr> <tr>
<td>End Point</td><td> <input type="text" name="e_point"></td> </tr>
<tr> <td> Latitude </td><td> <input type="text" name="lat"></td> </tr>
<tr> <td> Longitude </td><td> <input type="text" name="long"></td> </tr> </table>
<input type="submit" name="Define_Route" value="Define Route">
</form>
<br><br> </div>
</div>
对所有四列使用VARCHAR(255)
。
(在一个文件中使用)
<?php
session_start();
$link = mysqli_connect("localhost","root","123456","db_mmw") or die ("Error" .mysqli_errno($link));
if(isset($_POST['Define_Route'])){
// connection to mysql
$latitude=$_POST['lat'];
$longitude=$_POST['long'];
$start_point=$_POST['s_point'];
$end_point=$_POST['e_point'];
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO tbl_route (latitude,longitude,start_point,end_point) VALUES ('".$latitude."','".$longitude."','".$start_point."','".$end_point."')";
if (!mysqli_query($link,$sql))
{
die('Error: ' . mysqli_error($link));
}
} // brace for if(isset($_POST['Define_Route']))
mysqli_close($link);
?>
<div class ="form">
<form method="post" action=""><br> <table border="0">
<tr> <td>Start Point</td><td>
<input type="text" name="s_point"></td> </tr> <tr>
<td>End Point</td><td> <input type="text" name="e_point"></td> </tr>
<tr> <td> Latitude </td><td> <input type="text" name="lat"></td> </tr>
<tr> <td> Longitude </td><td> <input type="text" name="long"></td> </tr> </table>
<input type="submit" name="Define_Route" value="Define Route">
</form>
<br><br> </div>
</div>