Need help with 'Account Settings' page
Posted: Mon Jun 14, 2010 2:22 am
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.
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;
}
}
//++++++++++++++++++++++++++++++++++++