开发者

What is wrong with this query?

开发者 https://www.devze.com 2023-02-18 19:29 出处:网络
Fixed: $goodQuery = \"INSERT INTO table (Lat, Lon, Description, CurrentDate, CurrentTime) VALUES (\'$lat\', \'$lon\', \'$desc\', \'$date\', \'$time\')

Fixed:

$goodQuery = "INSERT INTO table (Lat, Lon, Description, CurrentDate, CurrentTime) VALUES ('$lat', '$lon', '$desc', '$date', '$time')
              ON DUPLICATE KEY UPDATE Description = CONCAT(Description, ', $desc'), CurrentDate = '$date', CurrentTime = '$time'";

I am trying to update a row after a duplicate is found using PHP and MySQL:

My table:

Lat| Lon | Description | Date | Time | SubmitCount |

I have my index of (Lat, Lon, Date) set to unique and would like to update the Description, Time and Date of the row if all those things are found.

The query I am using is:

$goodQuery = "INSERT INTO table (Lat, Lon, Description, Date, Time) VALUES ('$lat', '$lon', '$desc', '$date', '$time')ON DUPLICATE KEY UPDATE Description = CONCAT(Description, ' ,'$desc'), Date = '$date', Time = '$time";

$desc, $date and $time are obtained using:

 $desc =  mysql_real_escape_string($_POST['desc']);
 //Get date
 $date = gmdate("Y-m-d");
 //Get time
 $time = gmdate("H:i:s");

So 开发者_StackOverflow中文版what I am doing wrong as the row will update if I use any one of these on their own but will not when they are in combination.

Any help would be great thanks.


You appear to have an extra, or not enough quotes in the CONCAT(). Looks like you're trying to add a description after the existing Description by concatenating with an empty space, but the empty space doesn't get a closing quote. Try this:

$goodQuery = "INSERT INTO table (Lat, Lon, Description, Date, Time) VALUES ('$lat', '$lon', '$desc', '$date', '$time')ON DUPLICATE KEY UPDATE Description = CONCAT(Description, ' ','$desc'), Date = '$date', Time = '$time";

CONCAT(Description, ' ,'$desc')
// Becomes
CONCAT(Description, ' ','$desc')
0

精彩评论

暂无评论...
验证码 换一张
取 消