Problem with PHP arrays and detecting differences in them. Help please?

Jason

New member
Im working on making an Account Settings page and the way i detect if changes are made is a simple process.

1) Query database & get old info and store them in an array
2) Display the old info in each input, example: <input type="lastName" value="<?php echo $oldInfo['lastName'];?>" ?>
3) When form submitted, store form values in a new array
4) Compare $oldInfo with $accountSettings (current "new" values)
5) If any changes, update what is changed.

So i made a function to take 2 arrays, if any differences it goes through and takes out what is the SAME and keeps what is not the same in the $accountSettings variable. This way the changes are stored in an array as $newInfo['MySQLColumnName'] = 'Column value'. Everything was working fine til i found a problem in it. The only thing that messes this up is when the user leaves a field blank.

Example, lets say you decided not to show your location. So you delete your "location" info from the field, then submit the form. It then messes the function up and displays a message "no changes were made" which is a error message i have it echo if there were no changes. The problem is there was changes.

I think the issue has something to do with a part of the array having a value as an empty string or something... Please help me! Here is the function i made.

//++++++++++++++++++++++++++++++++++++
// 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, &$result) {
// 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;
}
}
//++++++++++++++++++++++++++++++++++++
 
Back
Top