PHP MySQL 语法错误

标签 php mysql

我制作了一个 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)情况。

在使用这些值之前:

  1. $username = $_GET['username'];
  2. $latitude = $_GET['latitude'];
  3. $longitude = $_GET['longitude'];
  4. ...

过滤转义准备它们以便对您的数据库进行安全查询。

最好的方法是使用PDO

关于PHP MySQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32434256/

相关文章:

php - zend_navigation 和模块

php - 复制行,同时忽略基于特定列的重复项

php - CodeIgniter 回调不起作用

php - Magento Product Collection GROUP BY 具有最低价格产品 ID 的最低价格?

mysql - 在 MySQL 中使用嵌套的 case 语句

php - MySQL查询: How to join multiple table with statment

php - 基于替代表查询的 SQL 查询表

python - 属性错误 : 'MySQLCursor' object has no attribute 'commit'

mysql - MySQL 中有类似 substr_replace 的东西吗?

php - 如何在php中设置获取数据的值