检查phonenumber是否等于,然后更新其他人

时间:2012-10-10 09:43:11

标签: php mysql

我是PHP的新手,我没有得到这个。

我想检查PHP文件,如果** $ id作为输入,id作为输入列**已经在数据库中,如果它已经... 然后应该更新所有其他值:经度,纬度,时间戳

这是我的代码:(只有重要的部分......)

file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" .   print_r($_GET, true));


// Performing SQL query
if (isset($_GET['data'])) {
$data = json_decode($_GET['data'], true);
$id = $data['id'];
$latitude = $data['latitude'];
$longitude = $data['longitude'];
$timestamp = $data['timestamp'];

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

// Free resultset
mysql_free_result($result);
}

3 个答案:

答案 0 :(得分:2)

这可以在单个SQL请求中完成。如果尚未完成,您需要在UNIQUE字段上创建id索引,然后完成以下请求:

INSERT INTO locatie (id, longitude, latitude, timestamp) 
    VALUES ('%s', '%s', '%s', '%s')
ON DUPLICATE KEY UPDATE 
    longitude=VALUES(longitude),
    latitude=VALUES(latitude),
    timestamp=VALUES(timestamp)

答案 1 :(得分:1)

逻辑就像这样

$sql = mysql_query("select * from table_name where phonenumber = your_phone number");
if(mysql_num_rows($sql) > 0)
{
    // your code for when phone number not present
}
else
{
   // your update query.
}

这只是逻辑。使用MysqliPDO代替mysql

答案 2 :(得分:0)

 first create sp like :
    CREATE PROCEDURE `SP_name`(IN `par1` INT, IN `par2` INT, IN `par3` Decimal)
    begin
      declare Rec_Count int ;   
    Proc:
      begin
         select count(*) into Rec_Count from  table UL where phoneno=par1;
       if(Rec_Count <= 0) then
         select '0' as success,'Phoneno is not found' as  response;
          leave Proc;
       end if;

        INSERT INTO locatie (col1,col2,...) VALUES (par1,par2,...);
        select '1' as success,'data successfully inserted.' as  response;
    end ;

    end

    ----------------
    then your code is :

  execute sp like from your code is :
$query="call sp_name(par1,par2,..);"
 $result = mysql_query($query) or die('Query failed: ' . mysql_error());