1

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

Jouri de Jong
  • 33
  • 1
  • 3
  • What are the values you are passing by POST? Please provide some sample data which you have passed. – RK12 Mar 08 '16 at 10:41
  • 1
    Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Henders Mar 08 '16 at 10:43
  • For example username => "test" password => "test" email => "test@test.test" – Jouri de Jong Mar 08 '16 at 10:44

3 Answers3

3

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);
gabe3886
  • 4,235
  • 3
  • 27
  • 31
0

$reg = $u->Createuser($u, $p, $e); replace this to $reg=$user->Createuser($u, $p, $e); change the name of object.

Domain
  • 11,562
  • 3
  • 23
  • 44
0

$u veriable is conflicting in this code. Please take another veriable :

replace this :

$u = $_POST['username'];

With

$username = $_POST['username'];
RK12
  • 472
  • 3
  • 11