doujianjian2060 2016-08-18 19:51
浏览 24
已采纳

调用bind_param()之后的Php,变量什么都不返回

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...

  • 写回答

1条回答 默认 最新

  • doukuipai8544 2016-08-18 20:08
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?