I have this code :
public function changeProfile ($first_name, $last_name, $email, $phone, $password,
$bank_name, $bank_account_number, $bank_account_name,
$gender, $birthday, $address, $area, $default_type) {
$this->connect();
$data = array ('first_name' => $this->escapeString($first_name),
'last_name' => $this->escapeString($last_name),
'email' => $this->escapeString($email),
'phone' => $this->escapeString($phone),
'password' => $this->escapeString($password),
'bank_name' => $this->escapeString($bank_name),
'bank_account_number' => $this->escapeString($bank_account_number),
'bank_account_name' => $this->escapeString($bank_account_name),
'gender' => $this->escapeString($gender),
'birthday' => $this->escapeString($birthday),
'address' => $this->escapeString($address),
'area' => $this->escapeString($area),
'default_type' => $this->escapeString($default_type));
$this->update('user', $data, 'email = "'.$email.'" AND phone = "'.$phone.'"');
$res = $this->getResult();
}
Now, I have a problem like this :
I want to 'skip' some variables on the function, for example $birthday and $gender, so that it won't be processed on SQL UPDATE and let the current data as it is.
I mean, if $birthday and $gender data doesn't exist, then don't update existing data on the table. because when I tried to use NULL as variables on the function, my data replaced with empty data.
how to manage this situation without having multiple if to check each variables?
thank you
If you have the option, you should replace your function's argument list with an array of arguments, for example:
public function changeProfile($variables)
{
$this->connect();
$data = array();
foreach ($variables as $key => $value) {
$data[$key] = $this->escapeString($value);
}
// Validation?
if (!isset($data['email'], $data['phone'])) {
die('Required fields missing.');
}
$this->update('user', $data, 'email = "' . $data['email'] . '" AND phone = "' . $data['phone'] . '"');
$res = $this->getResult();
}
This assumes that the SQL query is static. You could also add some more fields to it if you needed to.
You'd then call it like this:
$test->changeProfileUpdated([
'first_name' => 'john',
'last_name' => 'doe',
'email' => '[email protected]',
'phone' => 12345,
'password' => 'password1',
'bank_name' => 'ANZ',
'bank_account_number' => '123-456',
'bank_account_name' => 'J DOE',
'gender' => 'M',
'birthday' => '1/2/13',
'address' => '12 Fake St',
'area' => 'Area 51',
'default_type' => 'Some default'
]);
Here's a demo comparing your code, this example and a validation failure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With