Best way to Save "old data"

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Best way to Save "old data"

Post by jimstoffel » 2018-10-15 14:29

Creating a proof of concept database. Have sample data in DB; user gets to "view" the data; User can also "edit" (replace) said data already in the data fields. As part of the "saving" process (where current data is being overwritten with new data), would like to save any "old data" to a new table when the users clicks on the "SAVE" button.

Is this where using the "hook" files comes in?

I've attached a screenshot of what I mean.

And appreciate any comments/suggestions!!
Attachments
screenshot_001.png
screenshot_001.png (63.25 KiB) Viewed 4303 times

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Best way to Save "old data"

Post by pbottcher » 2018-10-15 15:03

Hi,

yes exactly. You can use the before_update function to save your current record, referenced by the $data['selectedID'](or the part of data you want to save) to the other table.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: Best way to Save "old data"

Post by jimstoffel » 2018-10-15 15:11

Thank you, pböttcher, for responding.

I thought as much, and tried placing code within said section, using exactly what you wrote as a test field to collect on. The problem is after clicking on the "save" button, I get a "500 Error response" - and the "old data" doesn't get put into the table, however the field *does* get updated.

See screenshots below.
Attachments
screenshot_003.png
screenshot_003.png (11.58 KiB) Viewed 4299 times
screenshot_002.png
screenshot_002.png (63.29 KiB) Viewed 4299 times

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: Best way to Save "old data"

Post by jimstoffel » 2018-10-15 15:13

CORRECTION: The *new* data does NOT get save. Sorry.

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: Best way to Save "old data"

Post by jimstoffel » 2018-10-15 16:14

OK, so I get data "saved" to the new table, however it is not the old data being grabbed but the new data. How do I ensure the OLD data is being grabbed prior to the NEW data being typed in when executing the BEFORE_INSERT hook section?

Also, I'm still getting the "500 - Internal server error." when I click on the SAVE button.

here's my code in that very section:

Code: Select all

function student_info_before_update(&$data, $memberInfo, &$args){

		//pull studentid# that is being updated along with additional data fields.
		$studid = $data['studentid'];
		$imm_data1 = $data['immunization_completed'];
		$imm_data2 = $data['immunization_additional'];
		$todaysdate = date("h:i:sa");
		
		sql("INSERT INTO student_immunization_info (studentid, immunization_completed, additional_immunizations) VALUES ('$studid', '$imm_data1', '$imm_data2')");

		return TRUE;
	}

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: Best way to Save "old data"

Post by jimstoffel » 2018-10-15 19:20

Alright, I figured out how to grab the "old" data to put into the new table (see code below) using the hook file, section "BEFORE_UPDATE".

However, I am still getting the "500 - Internal server error." when I click on the SAVE button. Any thoughts as to why?

Code: Select all

	function student_info_before_update(&$data, $memberInfo, &$args){

		//pull studentid# that is being updated along with additional data fields.
		$studid = $data['studentid'];
			
		// get the OLD data for future comparison
		$CurrentData01 = sqlValue("SELECT immunization_completed FROM student_info WHERE studentid = '$studid' ");
		$CurrentData02 = sqlValue("SELECT immunization_additional FROM student_info WHERE studentid = '$studid' ");
		
		// get the NEW data for comparison
		$NewData01 = $data['immunization_completed'];
		$NewData02 = $data['immunization_additional'];
		
		// check to see if data fields have changed
		if (($Currentdata01 != $NewData01) || ($Currentdata02 != $NewData02)) {
			$todaysdate = date("Y-m-d h:i:sa");
			sql("INSERT INTO student_immunization_info (studentid, immunization_completed, additional_immunizations, timestamp) VALUES ('$studid', '$CurrentData01', '$CurrentData02', '$todaysdate')");
		} else {
			return FALSE;
		}
		
		return TRUE;
	}

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Best way to Save "old data"

Post by pbottcher » 2018-10-15 19:45

Hi,

you need to grab the "old" data from the database, not from the $data array.

Try

Code: Select all

function student_info_before_update(&$data, $memberInfo, &$args){

		//pull studentid# that is being updated along with additional data fields.
		$studid = $data['studentID'];
                sqlvalue("INSERT  student_immunization_info s SELECT  t.studentid, t.immunization_completed, t.additional_immunizations FROM    student_info t
ON DUPLICATE KEY UPDATE s.immunization_completed = t.immunization_completed, additional_immunizations = t.additional_immunizations WHERE  
t.studentid = ".$studid);
		return TRUE;
	}
Assuming that studentid is a numeric field.

Note: sql requires as function call 2 arguments: first the sql statment, second an array to store the return code.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: Best way to Save "old data"

Post by jimstoffel » 2018-10-16 18:53

Hi pböttcher,

Yes, I figured out how to grab the "old" data (see my original code.

The problem is I am still getting the "500 - Internal server error" when I click on the SAVE button. I am currently logged in as "admin" during this build phase, so I know the admin account has access to "all" tables.

I have looked at the log file, and it references ajax_check_login.php each time the error page pops up. Guess I'll have to figure out the purpose of said file.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Best way to Save "old data"

Post by pbottcher » 2018-10-17 05:38

Hi,

can you remove once your code in the hook file and see if the save works fine. If yes, can you post the code you are using as I would assume there is an error which gives you the "500- ...."
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: Best way to Save "old data"

Post by jimstoffel » 2018-10-17 20:59

Had used just "sql" instead of "sqlvalue" which caused my "500..." error.
Code is now working as it suppose to. Thanks, pböttcher, for helping with this.

My code that I'm using that works as intended in my hook file (FILENAME_view.php) is:

Code: Select all

	function student_info_before_update(&$data, $memberInfo, &$args){

		//pull studentid# that is being updated along with additional data fields.
		$studid = $data['studentid'];
			
		// get the OLD data for comparison
		$OldData01 = sqlValue("SELECT immunization_completed FROM student_info WHERE studentid = '$studid' ");
		$OldData02 = sqlValue("SELECT immunization_additional FROM student_info WHERE studentid = '$studid' ");
		
		// get the NEW data for comparison
		$NewData01 = $data['immunization_completed'];
		$NewData02 = $data['immunization_additional'];
		
		// check to see if data fields have changed
		If ((($NewData01 == "") || ($NewData01 == null)) && (($NewData02 == "") || ($NewData02 == NULL))) {
			return false;
		} elseif (($OldData01 != $NewData01) || ($OldData02 != $NewData02)) {
			$todaysdate = date("Y-m-d h:i:sa");
			sqlvalue("INSERT INTO student_immunization_info (studentid, immunization_completed, additional_immunizations, timestamp) VALUES ('$studid', '$OldData01', '$OldData02', '$todaysdate')");
			return true;
		}
		
	}

jimstoffel
Posts: 22
Joined: 2017-01-13 02:43

Re: Best way to Save "old data"

Post by jimstoffel » 2018-10-17 21:02

Actually used this line instead, as easier to follow:

If (empty($NewData01) && empty($NewData02))

Post Reply