的PHP MySQL查询故障

问题描述:

我得到这个错误:的PHP MySQL查询故障

Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' ,,)' at line 2 

在此PHP代码:

<?php 

$id = $_POST['id']; 
$longitude = $_POST['longitude']; 
$latitude = $_POST['latitude']; 
$timestamp = $_POST['stringFromDate']; 



$link = mysql_connect('server', 'user', 'pass') 
or die('Could not connect: ' . mysql_error()); 

mysql_select_db('db_name') or die('Could not select database'); 

// Performing SQL query 
$query="INSERT INTO locatie (id, longitude, latitude, timestamp) 
VALUES ($id, $longitude,$latitude,$timestamp)"; 
$result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
echo "OK"; 

// Free resultset 
mysql_free_result($result); 

// Closing connection 
mysql_close($link); 
?> 

我是个初学者,所以我不知道我在做什么错

编辑: 这是写入php文件的代码:

- (void)myFuntionThatWritesToDatabaseInBackgroundWithLatitude:(NSString *)latitude longitude:(NSString *)longitude date: 

(NSString *)stringFromDate { 


_phonenumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"]; 

NSMutableString *postString = [NSMutableString stringWithString:kPostURL]; 
NSString*jsonString = [[NSString alloc] initWithFormat:@"{\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"}",_phonenumber, longitude , latitude, stringFromDate]; 

[postString appendString:[NSString stringWithFormat:@"?data=%@", jsonString]]; 
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString ]]; 
[request setHTTPMethod:@"POST"]; 

[[NSURLConnection alloc] initWithRequest:request delegate:self ]; 
NSLog(@"Post String =%@", postString); 


// LocationTestViewController*locationTestViewController = [[LocationTestViewController alloc]init]; 
// phonenumber = locationTestViewController.telefoonnummer; 
NSLog(@"HERE1 : %@", _phonenumber); 





} 
+2

您的变量不包含您期望它们的值,可能是因为'$ _POST'为空。在您尝试执行SQL事务之前,验证这些键是否存在于$ _POST中,并且是数字值。此外,这段代码容易受到SQL注入的影响。在最小的情况下,在每个变量上调用'mysql_real_escape_string()'。 –

+0

如果你'echo $ query;'你会看到类似'INSERT INTO locatie(id,longitude,latitude,timestamp)VALUES(,,,,)'' - 一堆空变量。 –

+0

http://*.com/questions/11093634/you-have-an-error-in-your-sql-syntax-check-the-manual-that-c​​orresponds-to-your –

也许你应该使用另一种符号并防止SQL注入。

最好的方法是使用预准备语句。

http://php.net/manual/de/pdo.prepared-statements.php

$id = $_POST['id']; 
$longitude = $_POST['longitude']; 
$latitude = $_POST['latitude']; 
$timestamp = $_POST['stringFromDate']; 

$stmt = $dbh->prepare("INSERT INTO locatie (id, longitude, latitude, timestamp) VALUES (?,?,?,?)"); 
$stmt->bindParam(1, $id); 
$stmt->bindParam(2, $longitude); 
$stmt->bindParam(3, $latitude); 
$stmt->bindParam(4, $timestamp); 
+0

我做了编辑,其中的ID,经度,纬度和时间戳来自 –

我真的建议像@MichaelBerkowski说的那样,检查POST数据的内容。 我敢肯定的是,这些瓦尔之一是完全空:

$id = $_POST['id']; 
$longitude = $_POST['longitude']; 
$latitude = $_POST['latitude']; 
$timestamp = $_POST['stringFromDate']; 
+0

确实。 Babboe:看看isset(...)。 –

+0

或空()函数,将帮助你很多:) –

+0

我不喜欢空()。在这种情况下,可能碰巧“工作”,但空()将在所有情况下返回true。例如:0作为整数,0.0作为浮点数,“0”作为字符串,都被认为是空的,而它们可以表示来自帖子的完全有效值。 –

问题数1:您正在使用mysql_*功能。这些有一个红色的警告框,告诉你不要使用它们的原因。

问题2:您没有转义您的数据。这使您很容易受到意外输入导致的XSS攻击和SQL语法错误的攻击。

问题3:您没有在SQL查询中引用您的数据。

所有这三个问题都可以通过using prepared statements and parameterized queries解决,它需要一个现代的API(解决问题1)并自动引用和转义数据(解决问题2和3)。

+0

我做了一个编辑,其中的ID,经度,纬度和时间戳来自.. –

你需要的,如果他们是文字加引号:

$query="INSERT INTO locatie (id, longitude, latitude, timestamp) 
VALUES ($id, '$longitude','$latitude','$timestamp')"; 

你应该把里面的单'报价

$查询变量=“INSERT INTO locatie (ID,经度,纬度,时间戳) VALUES('$ id','$ longitude','$ latitude','$ timestamp')“;

+0

我做了一个编辑,其中id,经度,纬度和时间戳来自 –