php - Duplicate entry '' for key 2 -
here php code gives me "duplicate entry '' key 2" error...
<?php $host = "localhost"; $user = "admin"; $pass = "123"; $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<br>"); $userid= mysql_real_escape_string($_post['userid']); $latitude= mysql_real_escape_string($_post['latitude']); $longitude= mysql_real_escape_string($_post['longitude']); //$time= mysql_real_escape_string($_post['time']); $db_select=mysql_select_db("new"); if(!$db_select){ die(mysql_error()); echo "error"; } $query= "insert location(userid, latitude, longitude ) values ('{$userid}', '{$latitude}', '{$longitude}'); " ; if($medo=mysql_query($query)){ header("localhost/filename"); exit; }else{ echo"<p> error</p>"; die(mysql_error()); }
i don't think there problem code. please help.
as suppose you'll want update if present, use syntax mysql request :
$query= "insert location (userid, latitude, longitude ) values ('{$userid}', '{$latitude}', '{$longitude}') on duplicate key update latitude='{$latitude}', longitude='{$longitude}';";
so when user exist, updated new coordinates
Comments
Post a Comment