Page 1 of 1

Best way to Insert via hook

Posted: 2018-10-10 15:13
by jimstoffel
Hello,

I've been trying to code with a hook file, more so the section "FILENAME_before_update(&$data, $memberInfo, &$args)" but doesn't work. My goal is to "copy data" from one table to another - before the fields get updated in the original table.

It isn't working in that the "filename" page comes up blank. My code in the hook file is:

$studid = '{$data['studentID']}';
$imm_data1 = '{$data['immunization_completed']}';
$imm_data2 = '{$data['immunization_additional']}';
$todaysdate = date("h:i:sa");

mysql_query("INSERT INTO student_immunization_info (studentid, immunization_completed, additional_immunizations) VALUES ('$studid', '$imm_data1', '$imm_data2')");

return true;

Am I to assume that the main reason is that the "hook code" is trying to "read" data that hasn't been pulled into the screen-view yet, because an "item" hasn't been selected?

If this is the case, what would you suggest as a better idea in copying data into a new table prior to an update?

Appreciate any comments/suggestions.

James.

Re: Best way to Insert via hook

Posted: 2018-10-10 16:31
by pbottcher
Hi,

can you try

Code: Select all

$studid = $data['studentID'];
$imm_data1 = $data['immunization_completed'];
$imm_data2 = $data['immunization_additional'];
$todaysdate = date("h:i:sa");

sqlvalue("INSERT INTO student_immunization_info (studentid, immunization_completed, additional_immunizations) VALUES ('$studid', '$imm_data1', '$imm_data2')");

return true;
The FILENAME_before_update should only be called on an update. So data should be filled (unless you clear all data :-) ).

Re: Best way to Insert via hook

Posted: 2018-10-10 17:19
by jimstoffel
Thank you for the suggestion, unfortunately that doesn't work.

Re: Best way to Insert via hook

Posted: 2018-10-10 17:37
by pbottcher
What happens? Can you post some more information?

Re: Best way to Insert via hook

Posted: 2018-10-10 17:56
by jimstoffel
As before, the FILENAME_info_view.php page comes up blank.

If I rem out the code, the page pops up as normal.

Re: Best way to Insert via hook

Posted: 2018-10-10 18:23
by jimstoffel
OK, I get my FILENAME_info_view.php back up by changing the code to:

$studid = $data['studentID'];
$imm_data1 = $data['immunization_completed'];
$imm_data2 = $data['immunization_additional'];
$todaysdate = date("h:i:sa");

mysql_query("INSERT INTO student_immunization_info (studentid, immunization_completed, additional_immunizations) VALUES ('$studid', '$imm_data1', '$imm_data2')");

return TRUE;

The NEW problem is now I get NO data to view. It says "No Matches Found"
Which makes me think that somehow the "Before_Update" function is pre-populating the 'studenid' field with BLANK.

Thoughts??

Re: Best way to Insert via hook

Posted: 2018-10-10 18:35
by pbottcher
Can you post a screenshot of this. Where is the message "No Matches Found" coming?

You select a record and change some values in the detail view and click the save button, correct?

Maybe you can explain what you try to acheive.

Re: Best way to Insert via hook

Posted: 2018-10-10 18:50
by jimstoffel
Again, in the FILENAME_info_view.php page, there should be a list of all the data in the database, however I get "No Matches Found" (see attachment). Which is why I think that the code in the "Before_Update" function is pre-populating the 'studenid' field with BLANK.

Re: Best way to Insert via hook

Posted: 2018-10-10 18:52
by jimstoffel
I was under the impression that the &$data (array) would be populated once a record was selected but I think as the (hook) functions "loads" at page time, those 'data' fields are being pre-populated with BLANK based on my code.

Re: Best way to Insert via hook

Posted: 2018-10-10 20:00
by pbottcher
Hi,

sorry but I still do not get your workflow. At the top you add to the hook to the FILENAME_before_update which implies that you would handle your data in the FILENAME_view.
In your SQL you put the data into the table student_immunization_info.
In the last entry you access the FILENAME_info_view.

How are those related to each other?

Re: Best way to Insert via hook

Posted: 2018-10-11 14:07
by jimstoffel
My FILENAME_info_view.php is now working.
Had an error in my code in the 'before_update" section in the hook file (FILENAME_info_view.php) - missing a quote
All set. Appreciate all the help.