Paul Heinemeyer Paul Heinemeyer - 3 months ago 11
PHP Question

Php after calling bind_param(), variable returns nothing

I created a method which should return some parameters like this:

public function registerUser($email, $first_name, $last_name, $password, $salt)
{
$sql = "insert into user_Anmeldung set email=?, first_name=?, last_name=?, user_password=?, salt=?";
$statement = $this->conn->prepare($sql);
echo $email;
echo $first_name;
echo $last_name;
echo $password;
echo $salt;

if (!$statement) {
echo "FAIL";
return;
}
else
{
$statement->bind_param("sssss", $email, $first_name, $last_name, $password, $salt);
$returnValue = $statement->execute();
}

return $returnValue;
}


here i call the method :

$result = $dao->registerUser($userEmail, $userFirstName, $userLastName, $secured_password, $salt);


and here i use the result :

if($result)
{
$userDetails = $dao->getUserDetails($userEmail);

$returnValue["status"]="200";

$returnValue["message"]="Successfully registered new user";

$returnValue["userId"] = $userDetails["user_id"];

$returnValue["userFirstName"] = $userDetails["first_name"];

$returnValue["userLastName"] = $userDetails["last_name"];

$returnValue["userEmail"] = $userDetails["email"];

} else {
$returnValue["status"]="400";

$returnValue["message"]="Could not register user with provided information";
}


The variables
$email
etc. arent empty but the $resultValue seems empty, because after calling a method which looks if the result is empty it returns true and i cant insert the information into my database...

Answer

There are several issues currently working against you and compounding your confusion. See the code below:

This assumes you are using MySQLi.

public function registerUser($email, $first_name, $last_name, $password, $salt)
{ 
    $sql = "insert into user_Anmeldung set email=?, first_name=?, last_name=?, user_password=?, salt=?";
    $prepare = $this->conn->prepare($sql);

    if($prepare)
    {
        if($prepare->bind_param("sssss", $email, $first_name, $last_name, $password, $salt))
        {
            if($prepare->execute())
            {
                return true;
            }
            else
            {
                echo 'Execute failed<br>';
            }
        }
        else
        {
            echo 'Bind failed<br>';
        }
    }
    else
    {
        echo 'Prepare failed<br>';
    }

    echo 'MySQL error: '.$prepare->error.'<br>';

    return false;
}

Now feel free to do this:

if($dao->registerUser($userEmail, $userFirstName, $userLastName, $secured_password, $salt) === true)
{
    echo 'Registration succeeded';
}
else
{
    echo 'Registration failed';
}

die(); // go no further