drv13270 2012-10-09 12:58
浏览 40
已采纳

PHP到mySQL查询错误

I get this error:

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

On this PHP code:

<?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);
?>

Im a beginner, so i dont know what im doing wrong

EDIT: This is the code that writes to the php file:

 - (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);





 }
  • 写回答

5条回答 默认 最新

  • dongyan3237 2012-10-09 13:07
    关注

    Perhaps you should use another notation and prevent SQL injections.

    The best way is to use prepared statements.

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答