我制作了一个 php 文件,其中的参数通过 GET 方法传递。 问题是当我传递参数时它说:
使用以下 URL 的参数:
http://www.akshay.site90.net/sendlats.php?username=rakesh&lat=30.13348419&longitude=77.28685067
MySQL query failedYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' longitude=77.28685067 WHERE `username`=rakesh' at line 1
我的Php文件的代码如下,请看:
<?php
$username = $_GET['username'];
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
$con = mysql_connect("mysql3.000webhost.com","a2418693_GCM","[passwordhere");
if(!$con){
die('MySQL connection failed'.mysql_error());
}
$db = mysql_select_db("a2418693_GCM",$con);
if(!$db){
die('Database selection failed'.mysql_error());
}
$sql = "UPDATE driver SET lat=$latitude, longitude=$longitude WHERE `username`=$username";
if(!mysql_query($sql, $con)){
die('MySQL query failed'.mysql_error());
}
mysql_close($con);
最佳答案
重要!
尽量避免 SQL 注入(inject)情况。
在使用这些值之前:
$username = $_GET['username'];
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
- ...
过滤、转义、准备它们以便对您的数据库进行安全查询。
最好的方法是使用PDO
关于PHP MySQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32434256/