You are here:  » Help with code


Help with code

Submitted by ROYW1000 on Fri, 2011-08-05 11:10 in

Hi

I am using this code but it does not seem to being updating anything. Can anyone see any errors as tried several variations but nothing is updating and im getting no errors. Is should only update and not create new records.

<?php
error_reporting
(E_ERROR);
ini_set("display_errors","1");
// Define Date Critereia
$year date("Y");
$month date("m");
$day date("d");
$underscore '_';
$dash '-';
// File Names
$file1 ='masterFreeStock.csv';
$file2 ='FreeStock.csv';
// Parser Files
require("MagicParser.php");
// Login to Database
mysql_connect("localhost""xxx""xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
function 
myRecordHandler($record)
{
/*Database Tables is the first item entered and the excel sheet data goes in at the end*/
$sql "
SELECT * FROM products WHERE code = '"
.mysql_real_escape_string$record["KIT_MASTER_CODE"] )."'";
$result mysql_query($sql) or die(mysql_error());
if (
mysql_num_rows($result)) // update scLevel
{
$sql "
UPDATE products SET
scLevel = '"
.mysql_real_escape_string$record["KIT_FREE_STOCK"] )."'
WHERE
code = '"
.mysql_real_escape_string$record["KIT_MASTER_CODE"] )."' ";
}
mysql_query($sql) or die(mysql_error());
}
MagicParser_parse("$year$underscore$month$underscore$day$unders$file1","myRecordHandler","csv|44|1|34");
echo 
"$year$dash$month$dash$day$underscore$file1";
?>

Thanks
Roy

Submitted by support on Fri, 2011-08-05 11:58

Hi Roy,

As a first step, add the output of MagicParser_getErrorMessage() just in case the filename construction isn't quite correct, e.g.

if (!MagicParser_parse("$year$underscore$month$underscore$day$unders$file1","myRecordHandler","csv|44|1|34"))
{
  print MagicParser_getErrorMessage();
}

Cheers,
David.
--
MagicParser.com

Submitted by ROYW1000 on Fri, 2011-08-05 12:07

Hi

No errors are showing.

I have also added a counter and this at the very bottom.

print "Processed ".$counter." records.";
print "Last SQL statement was: ".$sql."";

And it returns this

Processed 2853 records.

Last SQL statement was: UPDATE jss_products SET scLevel = '' WHERE code = 'XPR16H'

But nothing seems to be updating in the database.

Thanks
Roy

Submitted by ROYW1000 on Fri, 2011-08-05 12:10

Hi David

Also when I test the code on your demo area it shows this as the format string csv|44|1|34

However when I enter my registration details and create the code it shows this.

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
// This is where you write your code to process each record, such as loading a database
    // You can display the record contents using PHP's internal print_r() function:
    
print_r($record);
    
// The following code will print out each field in your sample data:
    
print $record["KIT_MASTER_ID"];
    print 
$record["KIT_MASTER_CODE"];
    print 
$record["KIT_MASTER_NAME"];
    print 
$record["KIT_FREE_STOCK "];
  }
  
MagicParser_parse("2011-08-05_masterFreeStock.csv","myRecordHandler","");
?>

Thanks
Roy

Submitted by support on Fri, 2011-08-05 12:18

Hi Roy,

I'll correct the source code generator tool, but notice that it is showing a SPACE on the end of the key for KIT_FREE_STOCK[SPACE]. This will be because of white space on the end of the header row of your feed prior to the new line.

However, in your code, the space is not present so that would explain the scLevel being empty in your last SQL statement:

UPDATE jss_products SET scLevel = '' WHERE code = 'XPR16H'

In your original code; if you REPLACE:

$sql = "
UPDATE products SET
scLevel = '".mysql_real_escape_string( $record["KIT_FREE_STOCK"] )."'
WHERE
code = '".mysql_real_escape_string( $record["KIT_MASTER_CODE"] )."' ";

...with:

$sql = "
UPDATE products SET
scLevel = '".mysql_real_escape_string( $record["KIT_FREE_STOCK "] )."'
WHERE
code = '".mysql_real_escape_string( $record["KIT_MASTER_CODE"] )."' ";

...that should do the trick!

Cheers,
David.