Page 1 of 1

Need help with 'Account Settings' page

Posted: Mon Jun 14, 2010 2:22 am
by Reloaded
Hey all! Im new to this community so hopefully you all can help me out and ill stick around and help others if i can :)

Im having trouble finishing up my Account Settings page. The normal account settings page from site to site allow fields to be empty or changed from content to being empty. I have 2 different arrays. 1 contains "old" stuff (from database so the form is auto filled in) and "new" stuff which was the stuff in the forms. I have a function that takes an array and validates every value that exists (validate works 100% fine). Then its passed to another function to check if there is changes. If there is it strips out what is the same then leaves what is changed and builds an SQL query on that. Now that you have a basic understanding of how my page works the problem im having is when the user deletes the content in the form and submits the form to clear the stored OPTIONAL data. My function that checks for changes gets messed up when it comes to an empty field. Can you please help me? Thank you!

Some examples of what the SQL will look like if there is empty fields.

--> In this SQL i erased data from a field and left it blank, then changed an "Interests" field to just "Computers". The field that was changed to completely blank is totally left out of the SQL query. And my custom error says "You didn't make any changes", that error is triggered if the below PHP function (bottom of post) is returned FALSE instead of returning a SQL query.
SQL -> UPDATE members SET interests='Computers' WHERE username='Reloaded'


--> In this SQL i erased data from a field and left it blank then submitted the form.
SQL -> Didnt return an SQL when it should have? :(

PLease help thanks.

Code: Select all

//++++++++++++++++++++++++++++++++++++
// Check if account settings changed
//

// This script takes 2 arrays, compares them
// and makes a SQL query with KEY as column name,
// array value as column value. Used for Account Settings.
function checkSettingsChange($formValues, $compareValues, $username) {
	/*foreach($formValues as $key => $value) {
		(($value == '') ? $formValues[$key] = NULL : $formValues[$key] = $value);
	}*/
	
	// If differences
	if($temp = array_diff($formValues, $compareValues)) {
		// Create 2 variables, 1 to store KEY (column name)
		// 1 to store VALUE (column value)
		$columnKey = '';
		$columnValue = '';
		
		// Loop through what was different, store the array key in $columnKey
		// Store the value in $columnValue
		// Concatenates with a tab
		foreach($temp as $key => $value) {
			$columnKey .= (($columnKey == '') ? $key : "\t" .$key);
			$columnValue .= (($columnValue == '') ? $value : "\t" .$value);
		}
		
		// Break up the 2 arrays above at the tabs
		$columnKeyArray = explode("\t", $columnKey);
		$columnValueArray = explode("\t", $columnValue);
		
		// Start putting together the query
		$result =  "UPDATE members SET ";
		// Loop throuch each of the arrays, these contain for values:
		// $columnKeyArray = database column name
		// $columnValue Array = database column value
		foreach($columnKeyArray as $columnKey) {
			$result .= $columnKey ."=";
			foreach($columnValueArray as $columnValue) {
				$result .= "'" .$columnValue ."'";
				// Get rid of the used Column value
				// Check if another exists so we know to insert a ", "
				array_splice($columnValueArray,0,1);
				if(isset($columnValueArray[0])) {
					$result .= ", ";
				}
				// Break, we don't want to continue and add 2 values to 1 key
				break;
			}
			// Get rid of the used Column name
			array_splice($columnKeyArray,0,1);
		}
		$result .= " WHERE username='" .$username ."'";
		return $result;
	}
	// If no differences
	else {
		return false;
	}
}
//++++++++++++++++++++++++++++++++++++

Re: Need help with 'Account Settings' page

Posted: Mon Jun 14, 2010 5:27 am
by Phoenixheart
Not sure if I got what you mean, but a MySQL UPDATE which don't change anything will set mysql_affected_rows() value to 0. You can use === (triple equal signs) to check for the value AND type of the function return.

Re: Need help with 'Account Settings' page

Posted: Mon Jun 14, 2010 6:18 pm
by Reloaded
Hmm not sure if that is quiet the problem. The problem im having is the function gets messed up when it comes to a empty field.

for example the sql query when it comes across 2 empty fields will look like this:
UPDATE members SET firstName='', lastName= WHERE username='Reloaded'

As you can see the lastName doesn't get its ''

Re: Need help with 'Account Settings' page

Posted: Mon Jun 14, 2010 9:27 pm
by Phoenixheart
I would suggest you try var_dump() inside your foreach() loop and see where the problem is.

Re: Need help with 'Account Settings' page

Posted: Tue Jun 15, 2010 2:54 am
by Reloaded
Thank you i will try that now, I will edit this post and give the results... :)

Re: Need help with 'Account Settings' page

Posted: Tue Jun 15, 2010 5:34 am
by Reloaded
Ok so i didn't try what you said cus i figured out the problem a little before i was about to try it. Now my problem is this.

array_diff() won't recognize an array value that is blank ( '' ) it seems...

Btw the reason why the SQL would get messed up when a blank string was passed through was because i have the values seperated with a TAB and when i explode at the TABS there is nothing left. So that "nothing" was messing it up. I fixed it by doing this, but this way only works once ina blue moon so it seems something is wrong, and im 99% sure its array_diff() isn't doing something i think its supposed to be doing...

Anyways my fix for coming across blank strings is this..

Code: Select all

foreach($temp as $key => $value) {
	(($value == '' || is_null($value) || empty($value)) ? $temp[$key] = ' ' : $temp[$key] = $value);
}

// Then generate the query similar to this way:
$result = "UPDATE members SET column=TRIM('value')";
Please help me i really want to get passed this, and if i can figure this out i can finish my other pages that uses the same method as this :(