This topic is locked
[SOLVED]

 Update a table with import function

9/25/2014 3:12:27 PM
PHPRunner General questions
W
wrjost author

Hi,

I am trying to update an existing table by importing data from a csv-file. Normally, import function ADDS new records.
Any ideas if / how this can be achieved?


Record 2144 shows existing data before update, 2561 is the new imported record.
Thanks,

Wilfried

admin 9/25/2014

If you import record with primary key field value that is already in the database - it will update the existing record in this case.

W
wrjost author 9/25/2014

Problem is that I have two different primary key fields:

The first column is the primary key field of the existing table.

The primary key field of the imported file is "Tblteam_index".

admin 9/25/2014

This doesn't make any sense. The imported file cannot have any primary keys, this is just a set of records. Only database table can have a primary key field.
I believe you need to change the primary key selection for this table in PHPRunner project so they do match. Another option - implement your own code to check if specific record exists and if it does - update it.
This code needs to be added to BeforeInsert event:

http://xlinesoft.com/phprunner/docs/before_insert_record.htm

W
wrjost author 9/26/2014

I'm going for your second suggestion, Sergey, by inserting this code as the BeforeInsert event:

//********** Check if specific record exists ************

global $conn;

$strSQLExists = "select * from _tblteammeldungturnier where ((tblturnier_index=142) and (tblteam_index=17))";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

echo "record exists";

}

else

{

echo "record does not exist";

}


The result (when I try to import one single value via "copy and paste text"):

  • no message is displayed on the screen
  • the import screen displays "Processing records ....." endlessly (but "back to list" functions properly)
  • a blank record is added to the table


What am I doing wrong now? (The above code normally yields record no. 2144 as its query result. I've manually deleted 2561, since the combination of "tblturnier_index" and "tblteam_index" must be unique.)
Greetings,

W.

admin 9/26/2014

You won't see anything outputted from this event as it's executed via AJAX (you can only see it via Chrome Developer Tools or via Firebug). Also, according to the manual you need to return true if you want to proceed with adding new record and false if you don't.
Something like this:

global $conn;

$strSQLExists = "select * from _tblteammeldungturnier where ((tblturnier_index=142) and (tblteam_index=17))";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

return false;

}

else

{

return true;

}