title: Insert multiple data as well as update in database using php?
body: <p>I get Nearest 50 km location names from current location using google api, so it' works fine.</p>
  
  <p>So I need to insert all these locations into my database. If some location already there in database, I need to update these location.</p>
  
  <p>For example I get 10 locations in google api so 5 locations are already there in my database. I need to 5 location are update and remaining 5 locations are insert. </p>
  
  <p>Here is my code: </p>
  
  <pre><code>&lt;?php
  require 'dbconnect.php';
  $LocaName=$_REQUEST['locname'];
  $address=$_REQUEST['address'];
  $latt=$_REQUEST['Latt'];
  $long=$_REQUEST['Long'];
  
  if($latt &amp;&amp; $long)
  {
      $LocaNamearray = explode("|||", $LocaName);
      $addressarray = explode("|||", $address);
      $lattarray=explode("|||",$latt);
      $longarray=explode("|||",$long);
  
      for($i=0;$i&lt;count($lattarray);$i++)
      {
          $query1="select * from tbl_MapDetails where Latitude='".$lattarray[$i]."'and Longitude='".$longarray[$i]."'";
          $result1=mysql_query($query1);
          $now=mysql_num_rows($result1);
      }
  
      if($now &gt;=1)
      {
          for($k=0;$k&lt;count($lattarray);$k++)
          {
              $query="update tbl_MapDetails set LocationName='".$LocaNamearray[$k]."', Address='".$addressarray[$k]."',Latitude='".$lattarray[$k]."', Longitude='".$longarray[$k]."' where Latitude='".$lattarray[$k]."'and Longitude='".$longarray[$k]."'";
          }
          $nav="update";
      }
      else 
      {
  
          $query ="INSERT INTO tbl_MapDetails(LocationName,Address,Latitude,Longitude) VALUES";
          $strDelimiter = "";
  
          for($j=0;$j&lt;count($LocaNamearray);$j++)
          {
              $name =$LocaNamearray[$j];
              $address =$addressarray[$j];
              $lat = $lattarray[$j];
              $long = $longarray[$j];
              $query .= $strDelimiter."('$name', '$address','$lat','$long')";
              $strDelimiter = ',';
          }
          $nav="Add";
      }
  
      $result= mysql_query($query);
  
      if($result)
      {
          echo mysql_error();
          $message=array("message"=&gt;"sucessfully".$nav);
      }
      else
      {
          echo mysql_error();
          $message=array("message"=&gt;"fail".$nav);
      }
  }
  else
  {
      $message=array("message"=&gt;"require latt and long");
  }
  
  echo json_encode($message);
  
  ?&gt;
  </code></pre>
  
  <p>Here insert and update working but I need to check every location in database. There is no location in database. It need to insert other location are update. how to check both these conditions matched locations are update and unmatched locations are inserted  Please guide me.</p>