Jump to content

Update record if exists...Insert if not


esiason14

Recommended Posts

I'm parsing my pages to find all id's. What I want to do is: if the id already exists in my db then I want to update it...if not I want to insert a record. How can I go about doing that. Any help would be appreciated. Thanks!!

 

foreach ($url as $value) 
      {
          $base = "http://www.mypage.com/whatever";
          $getit = "".$base."$value";
      	  $input = file_get_contents("$getit");	 
preg_match_all('@<a href="/whatever/([A-Za-z0-9-]+)">(.*?), (.*?)</a>@', $input, $matches);


  for($i=0;$i<40;$i++)
  {	
     $sql3 = "SELECT id from mlbplayers where id=".$matches[1][$i].""; 
        while ($row3 = mysql_fetch_assoc($result3)) 
  {			
		$pid = $row3["id"];
  } 
echo "$sql3<br />";
      }
  }

Link to comment
https://forums.phpfreaks.com/topic/45139-update-record-if-existsinsert-if-not/
Share on other sites

 $sql3 = "SELECT id from mlbplayers where id=".$matches[1][$i].""; 

You cannot have code like this. You're confusing the PHP with double quotes IN double quotes.

 

You should have it like this.

 

 $sql3 = "SELECT id from mlbplayers where id='.$matches[1][$i].'";

 

When you cannot use double quotes in html, php, sql, you usually use a alternative like '.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.