Page 1 of 1

Best way to Save "old data"

Posted: 2018-10-15 14:29
by jimstoffel
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!!

Re: Best way to Save "old data"

Posted: 2018-10-15 15:03
by pbottcher
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.

Re: Best way to Save "old data"

Posted: 2018-10-15 15:11
by jimstoffel
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.

Re: Best way to Save "old data"

Posted: 2018-10-15 15:13
by jimstoffel
CORRECTION: The *new* data does NOT get save. Sorry.

Re: Best way to Save "old data"

Posted: 2018-10-15 16:14
by jimstoffel
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;
	}

Re: Best way to Save "old data"

Posted: 2018-10-15 19:20
by jimstoffel
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;
	}

Re: Best way to Save "old data"

Posted: 2018-10-15 19:45
by pbottcher
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.

Re: Best way to Save "old data"

Posted: 2018-10-16 18:53
by jimstoffel
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.

Re: Best way to Save "old data"

Posted: 2018-10-17 05:38
by pbottcher
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- ...."

Re: Best way to Save "old data"

Posted: 2018-10-17 20:59
by jimstoffel
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;
		}
		
	}

Re: Best way to Save "old data"

Posted: 2018-10-17 21:02
by jimstoffel
Actually used this line instead, as easier to follow:

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