Description of problem:
I want to add values to a log that will show me which fields a user has changed on a form (during an update). This would be something like an audit trail for a database, but not so complex.
What I've thought up is a function that allows you to pass the entire array of posted values (new values) (values from the form's textboxes). This function will then reach into the database and create an array of values that already exist (old values).
Now we need to see which values the user has actually changed. What I have come up with in my code below partially works, and this is where I'm asking for help.
When the field is blank or something is entered into it, the code works fine. When you remove text, and leave the field blank, it doesn't show up.
Also, with radio buttons, since they are arrays, they are also always showing up as differences - and therefore being logged incorrectly to the log. I think I'm on the right track to fixing this, but I've commented that part out so I can just view some debug text.
Please bear with me as I have only been working with PHP for a few weeks. I am constantly trying to learn how to do things better and easier. What I have tried to do is use the array_diff function to try and make it easier for me to not do the for...each loops, although, if I should be using that, go ahead and let me know my logic is stupid, and I'll try something else.
Thanks in advance for your time.
Code:
/**
* Add new entry into the model log
*
* @access public
* @param array $PostedValues
* @return void
*/
function Add_1( $PostedValues )
{
$ModelID = $PostedValues['modelid'];
foreach ($PostedValues as $key => $value)
{
if($key!='submit')
{
${$key} = $value;
if($value)
{
if(!is_array($value))
{
$sqlvar .= addslashes($key).",";
//echo "Key: " . addslashes($key) . "<br />";
//echo "Value: " . addslashes($value) . "<br />";
} else {
$sqlvar .= addslashes($key).",";
}
}
}
}
$sql = "SELECT ".substr($sqlvar, 0, -1)." FROM model WHERE modelid = " . $ModelID;
//echo "<br /><br /><br /><br /><br /><b>Select Model SQL:</b> " . $sql . "<br />";
$query_results = mysql_query($sql);
$results = mysql_fetch_array($query_results);
echo "<b>Result Count:</b> " . count($results) . "<br />";
echo "<b>Posted Count:</b> " . count($PostedValues) . "<br />";
echo "<b>Differences:</b>";
$UpdatedValues = array_diff($PostedValues,$results);
//$bUpdatedValues = array_diff($results,$PostedValues);
print_r($UpdatedValues);
//print_r($bUpdatedValues);
echo "<br />";
foreach ($UpdatedValues as $key => $value)
{
if($key!='submit')
{
${$key} = $value;
if($value)
{
if(!is_array($value))
{
$sql = "INSERT INTO modellog (modelid,userid,field_updated,old_value,new_value,date_updated)
VALUES ($ModelID,".$_SESSION['userid'].",'".addslashes($key)."','".$results[addslashes($key)]."','".$PostedValues[addslashes($key)]."',now())";
//$junk = mysql_query($sql);
echo "<br /><b>ModelLog Add SQL:</b> " . $sql . "<br />";
//$sqlvar .= addslashes($key).",";
//echo "Key: " . addslashes($key) . "<br />";
//echo "Value: " . addslashes($value) . "<br />";
} /*else {
$ArrayUpdatedValues = array_diff_assoc($PostedValues,$value);
if($ArrayUpdatedValues && !is_array($ArrayUpdatedValues))
{
echo "<br /><b>Array:</b> ";
print_r($ArrayUpdatedValues);
echo "<br />";
$sql = "INSERT INTO modellog (modelid,userid,field_updated,old_value,new_value,date_updated)
VALUES ($ModelID,".$_SESSION['userid'].",'".addslashes($key)."','".$results[addslashes($key)]."','".implode(",", $PostedValues[addslashes($key)])."',now())";
//$junk = mysql_query($sql);
echo "<b>ModelLog Add SQL:</b> " . $sql . "<br />";
}
}*/
}
}
}
}