0

I am trying to right a script to update users in my database here is my code:

I get the user data from a form:

$user = new User();

if (isset($_POST['submit'])) {
  // Process the form

  // validations
  // $required_fields = array("username", "password");
  // validate_presences($required_fields);

  // $fields_with_max_lengths = array("username" => 30);
  // validate_max_lengths($fields_with_max_lengths);

  $errors = ""; //temp

  if (empty($errors)) {

    $user->username = $db->mysql_prep($_POST["username"]);
    $user->hashed_password = ($_POST["password"]);
    $user->first_name = $db->mysql_prep($_POST["first_name"]);
    $user->last_name = $db->mysql_prep($_POST["last_name"]);
    $user->id = $db->mysql_prep($_POST["id"]);

    print_r($user);

    $user_by_id = $user->find_user_by_id($user->id);

    print_r($user_by_id);

    $result = $user->change_user_by_id($user_by_id);

    //->id,$user->username,$user->hashed_password,$user->firstname,$user->lastname

    unset($user);
}

Here is my find_user_by_id method:

public static function find_user_by_id($id=0){

global $db; 

$query  = "SELECT * ";
$query .= "FROM users ";
$query .= "WHERE id = {$id} ";
$query .= "LIMIT 1";

$user_set = mysqli_query($db->connection, $query);
$db->confirm_query($user_set);

if($user = mysqli_fetch_object($user_set)) {
    return $user;
} else {
    return null;
}   
}

And finally my change_user_by_id method:

public function change_user_by_id($user){
    global $db; 
    global $session;

    $query  = "UPDATE users SET ";
    $query .= "username = '{$user->username}', ";
    $query .= "first_name = '{$user->first_name}', ";
    $query .= "last_name = '{$user->last_name}' ";
    $query .= "WHERE id = {$user->id} ";
    $query .= "LIMIT 1";

    $result = mysqli_query($db->connection, $query);
    $db->confirm_query($result);

    if ($result && mysqli_affected_rows($db->connection) == 1) {
          // Success
        $session->message("User updated.");
        redirect_to("list.php");
    } else {
          // Failure
        $session->message("User update failed.");
        redirect_to("change.php?id={$user->id}");

    }
}   

When I try to update a user I get redirected to change.php with the error message user update failed. I've been trying to work through the logic and I just can't seem to find where I've gone wrong. The print_r were added later to see if the objects had the proper values, and they did.

EDIT 1:

I changed some code to see what mysqli_affected_rows returns and it returns 0.

Praveen Perera
  • 158
  • 3
  • 13
  • You won't get an answer for offtopic question, no matter how many times you post a duplicate. – Your Common Sense Feb 22 '14 at 19:33
  • This isn't a duplicate question, this a different problem. And what do you mean by off topic? I think I figured out my first problem. I think it was caused by using the username to get the id, which means changing the username meant no id could be retrieved. So I changed my code and added a hidden field for id in my form. But I can't be sure if this fixed the first problem because it caused this second problem. – Praveen Perera Feb 22 '14 at 19:44

1 Answers1

0

After many hours I finally figured it out i just had to take out the line:

$user_by_id = $user->find_user_by_id($user->id);

and change the line:

$result = $user->change_user_by_id($user_by_id);

to:

 $result = $user->change_user_by_id($user);

This line was a remnant of the way I was doing it before and it caused my to send the original data instead of the new data to the change method.

Community
  • 1
  • 1
Praveen Perera
  • 158
  • 3
  • 13