Checking results before inserting MySQL

I’m trying to get the details from a cvs file into a database but I also want to check if the results are there already. If they are then I want to update them if not I want to then insert them.

This is what I’ve got but every time I try I get a 500 error

$query="select feed,file,name from dealer where enabled = 1";
    $csv_convert=dbselect( $query,"dbLinkInt" );
	if( is_array( $csv_convert ) ) { 
	foreach( $csv_convert as $key=>$value ) { 
	$url = $value['feed'];

 file_put_contents("".$value['filename'].".csv",gzdecode(file_get_contents($url)));
  
  $handle = fopen($value['filename'].".csv", "r");

	while (($data = fgetcsv( $handle )) !== FALSE) {
	$gender="0";
		  
      # create product
	  $query="select dealer_product_id from feeds";
      $productid=dbupdate( $query,"dbLinkInt" );
	  if ( $productid!=$data[0] ) {
      $query="insert into feeds set dealer_product_id =".dbstr( $data[0] ).", dealer_link =".dbstr( $data[3] ).",image_url=".dbstr( $data[4] ).",price=".dbstr( $data[5] ).",product_name=".dbstr( $data[7] ).",product_url=".dbstr( $data[8] ).",delivery_cost=".dbstr( $data[14] ).",ean=".dbstr( $data[19] ).",product_brand=".dbstr( $data[20] ).",brand_name=".dbstr( $value['name'] ).",promo_text=".dbstr( $data[21] ).",large_image=".dbstr( $data[22] ).",enabled=".dbstr( $data[23] );
      $newid=dbupdate( $query,"dbLinkInt" );
      $query="insert into text_store set tablename='product_detail', owner_id=$newid, type='paragraph', text=".dbstr( $data[6] ).", instance=1";
    dbupdate( $query,"dbLinkInt" ) or die(mysql_error());
	} else {
      $query="update feeds set dealer_link=".dbstr( $data[3] ).",image_url=".dbstr( $data[4] ).",price=".dbstr( $data[5] ).",product_name=".dbstr( $data[7] ).",product_url=".dbstr( $data[8] ).",delivery_cost=".dbstr( $data[14] ).",ean=".dbstr( $data[19] ).",product_brand=".dbstr( $data[20] ).",brand_name=".dbstr( $value['name'] ).",promo_text=".dbstr( $data[21] ).",large_image=".dbstr( $data[22] ).",enabled=".dbstr( $data[23] )." where aw_product_id=".dbstr( $data[0] );
      $query="update text_store set tablename='product_detail', owner_id=".dbstr( $data[0] ).", type='paragraph', text=".dbstr( $data[6] ).", instance=1";
    dbupdate( $query,"dbLinkInt" ) or die(mysql_error());
	}
	}
	fclose($handle);
  } } 

	print "Import done";

There is INSERT-UPDATE syntax to handle such situations

Thanks, I’ll have a read of that

Are you using the old mysql_* extension? if you are you really need to be migrating away from that to either the mysqli_* extension or PDO. The old mysql_* extension was deprecated in version 5.5 of PHP and is being removed in version 7.

I’m using mysql although I believe the server is still running php 5 (not sure which version though).

phpversion()

That function will return what version of PHP is in use

Thanks, I’m running version 5.4.44

The mysqli_ version was introduced back in the original version 5 so it was around for quite a few years before the version you are currently using was released.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.