doutang1946 2016-03-08 10:35
浏览 36
已采纳

致命错误:在字符串上调用成员函数Createuser

Whenever i try to call function Createuser() it gives me this error

Fatal error: Call to a member function Createuser() on string

I really don't know why because my other functions all work.

The code where i call the function:

 $u = $_POST['username'];
  $p = $_POST['password'];
  $e = $_POST['email'];

  // attempt to create user
  $reg = $u->Createuser($u, $p, $e);

  // if an error code is sent back, it will put it in a session.
  switch ($reg) 
  {

    case 2:
      $_SESSION['err'] = 2;
      break;

    case 3:
      $_SESSION['err'] = 3;
      break;

    case 10:
      $_SESSION['err'] = 10;
      break;

And the function itself:

function Createuser($username, $password, $email)
    {
        include("connection.php");

        $info = mysqli_query($db2, "SELECT * FROM users WHERE username = '$username' AND password = '$password'");

        // If any field is not empty 
        if(($username != NULL) && ($password != NULL) && ($email != NULL))
        {
            // If the result matches break the process
            if(mysqli_num_rows($info) == 0)
            {
                // If rank is defined make it rank 0 else rank equels rank
                if($rank == NULL)
                {
                    $rank = 0;
                }
                else
                {
                    $rank = $rank;
                }

                // Insert into $table
                if(mysqli_query($db2, "INSERT INTO users VALUES(NULL, '$username', '$password', '$email', 0)"))
                {
                    return 10;
                }
            }
            else
            {
                // Return error code 3
                return 3;
            }
        }
        else
        {   
            // Return error code 2
            return 2;
        }
    }

I really hope someone could help me with this..

  • 写回答

3条回答 默认 最新

  • douxia2137 2016-03-08 10:49
    关注

    The issue you are having comes from the following section of code:

    $u = $_POST['username'];
    $p = $_POST['password'];
    $e = $_POST['email'];
    
    // attempt to create user
    $reg = $u->Createuser($u, $p, $e);
    

    You have $u being set to a string by $_POST['username'], and are then trying to call a function on that string.

    If you've previously created a variable $u which is a class User (as mentioned in a comment), then by doing $u = $_POST['username'];, you've destroyed the object and set it as a string.

    You'll need to do something like:

    $user = new User(); // or however the user object is created
    $u = $_POST['username'];
    $p = $_POST['password'];
    $e = $_POST['email'];
    
    // attempt to create user
    $reg = $user->Createuser($u, $p, $e);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀