[Solved] Removing eval from PHP function [closed]


It seems like you could simplify your code a lot, and remove the need for eval() which you shouldn’t use unless it is a last resort.

There is no need for all the IF blocks had in your code, because if the value isn’t set, it also won’t be added to the $values array.

Just assign your $_POST variable to a $values variable, Laravel does most of the heavy lifting for you.

public function alterUser()
{

    $values = $_POST;

    //remove _token variable created by Laravel in all POST requests
    unset($values['_token']); 

    //perform any actions needed on values before being send to database
    $values['password'] = Hash::make($values['password']);

    if(!empty($values['id'])) {
        DB::table('users')->insert($values);
    } else {
        DB::table('users')->where('id', $values['id'])->update($values);
    }
    
    return redirect('/user');
}

I see in your code that you rename the contact variable to contacto. I recommend changing your form to match this variable name, but if that isn’t possible, you can still rename it after setting $values = $_POST like this:

$values['contacto'] = $values['contact'];
unset($values['contact']);

Also, if your form sends any variables that you do NOT want to send to the database, such as a “password verify” field or something like that, then you can unset them after setting $values = $_POST like this:

unset($values['VALUE_TO_REMOVE']);

15

solved Removing eval from PHP function [closed]