Jump to content

Checking results before inserting


sonicthehedgehog

Recommended Posts

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";

If I don't bother checking the results before hand then I can insert them no problems it's just when I try and see if they already exist I get stuck

Link to comment
Share on other sites

thanks, I tried that but still didn't get anything. This is the code I've got at the moment

 ini_set('display_errors', 1);error_reporting(E_ALL);$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="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";
If I add my own information to the query (i.e.: dealer='www.domain.com') it works but when I try to do it from the feed (which is the same feed I used to add the products nothing happens
Edited by sonicthehedgehog
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...