1

Hi guys i have a code for update my database

$users = Sdba::table('tabusuarios');
$users->where('User_Id =', $User_Id);
$data = array('User_Name'=> $User_Name, 'User_Email'=> $User_Email, 'User_Status'=> $User_Status, 'User_Perm'=> $User_Perm);
$users->update($data);

My problem is i want to also update 1 more column if this exists something like this

if ($_POST["User_Password"]) $data = array('User_Password'=> saltSenha($_POST["User_Password"]));

So i want to make a all this togheter

Thanks for any help.

1
  • Why not just say if (!empty($_POST...)) { $data['User_Password'] = ...; } ? Commented Dec 6, 2013 at 3:46

4 Answers 4

1

Make use of the isset construct and do like this

$users = Sdba::table('tabusuarios');
$users->where('User_Id =', $User_Id);
$data = array('User_Name'=> $User_Name, 'User_Email'=> $User_Email, 'User_Status'=> $User_Status, 'User_Perm'=> $User_Perm);

if(isset($_POST["User_Password"]) && !empty($_POST["User_Password"]))
{
    $data['User_Password']=saltSenha($_POST["User_Password"]);
}

$users->update($data);
Sign up to request clarification or add additional context in comments.

Comments

0
$users = Sdba::table('tabusuarios');
$users->where('User_Id =', $User_Id);
if ($_POST["User_Password"]) {
    $data = array('User_Name'=> $User_Name, 'User_Email'=> $User_Email, 'User_Status'=> $User_Status, 'User_Perm'=> $User_Perm, 'User_Password'=> saltSenha($_POST["User_Password"]));
}else{
    $data = array('User_Name'=> $User_Name, 'User_Email'=> $User_Email, 'User_Status'=> $User_Status, 'User_Perm'=> $User_Perm);
}
$users->update($data);

2 Comments

Repetition sucks. :P
well said. scope for improvement is always there. the motto is to make a novice understand how to do it :)
0

Just do this:

$users = Sdba::table('tabusuarios');
$users->where('User_Id =', $User_Id);
$data = array('User_Name'=> $User_Name, 'User_Email'=> $User_Email, 'User_Status'=> 

if ($_POST["User_Password"]){
    $data['User_Password'] = saltSenha($_POST["User_Password"]);
}

$User_Status, 'User_Perm'=> $User_Perm);
$users->update($data);

Comments

0

Just set the base $data array and then if $_POST["User_Password"] exists & is not empty, just add that to the array? Example based on your code below. I find that using a double checking mechanism of array_key_exists() coupled with a !empty(trim()) is the best way to ensure that you are not dealing with junk data or whitespaces in a passed variable like $_POST, $_REQUEST or $_GET:

$users = Sdba::table('tabusuarios');
$users->where('User_Id =', $User_Id);
$data = array('User_Name'=> $User_Name, 'User_Email'=> $User_Email, 'User_Status'=> $User_Status, 'User_Perm'=> $User_Perm);
if (array_key_exists("User_Password", $_POST) && !empty(trim($_POST["User_Password"])) {
  $data['User_Password'] = saltSenha($_POST["User_Password"]);
}
$users->update($data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.