Skip to content
Advertisement

Fatal error: Uncaught ArgumentCountError: Too few arguments to function

I know there was a some questions related to this, but there are in c++ or other languages. I get this error and I’m not sure what is wrong with my function.

My error looks like this:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function User::register(), 2 passed in C:xampphtdocsregister.php on line 39 and exactly 5 expected in C:xampphtdocsclassesusers.php:22 Stack trace: #0 C:xampphtdocsregister.php(39): User->register('ds', 'dsssssss') #1 {main} thrown in C:xampphtdocsclassesusers.php on line 22

And my function is:

public function register($name, $surname, $username, $password, $email)
{
    try {
        $newPassword = password_hash($password, PASSWORD_DEFAULT);

        $stmt = $this->conn->prepare("INSERT INTO user(name, surname, username, password, email) 
                             VALUES(:name, :surname, :username, :password, :email)");

        $stmt->bindParam(":name", $name);
        $stmt->bindParam(":surname", $surname);
        $stmt->bindParam(":username", $username);   
        $stmt->bindParam(":password", $password);
        $stmt->bindParam(":password", $password);
        $stmt->bindParam(":email", $email);

        $stmt->execute();   

        return $stmt;   
}
    catch(PDOException $e) {
            echo $e->getMessage();
    }
}

Register.php file:

  <!DOCTYPE html>
<?php
session_start();
require_once('classes/users.php');
$user = new User();

if($user->isLoggedIn()!="") {
    $user->redirect('home.php');
}

if(isset($_POST['submit'])) {
    $name = strip_tags($_POST['name']);
    $surname = strip_tags($_POST['surname']);
    $username = strip_tags($_POST['username']);
    $password = strip_tags($_POST['password']);
    $email = strip_tags($_POST['email']);

if($name=="") {
        $error[] = "provide username !";    
} else if($surname=="") {
        $error[] = "Provide surname!";  
  } else if ($username =="") {
      $error[] = "Provide username!";
    } else if($password=="") {
        $error[] = "provide password !";
      } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error[] = 'Please enter a valid email address !';
        } else if(strlen($password) < 6){
            $error[] = "Password must be atleast 6 characters"; 
          } else {
                try {
                    $stmt = $user->runQuery("SELECT username FROM user WHERE username=:username");
                    $stmt->execute(array(':username'=>$username));
                    $row=$stmt->fetch(PDO::FETCH_ASSOC);

                    if($row['username']==$username) {
                            $error[] = "sorry username already taken !";
                    } else {
                        if($user->register($username,$password)){   
                            $user->redirect('register.php?joined');
                        }
                    }
                }
                catch(PDOException $e) {
                    echo $e->getMessage();
                }
            }   
}
?>
<html>
<head>
    <meta charset="UTF-8">
    <title>
    </title>
</head>
<body>
    <div class="form">
        <form method ="post" action="register.php">
            <h3 class = "signup"> Sign Up </h3>
             <?php
        if(isset($error)) {
                        foreach($error as $error)
         {
                 ?>
                               <div class="alert alert-danger">
                    <i class="glyphicon glyphicon-warning-sign"></i> &nbsp; <?php echo $error; ?>
                 </div>
                 <?php
            }
        }
        else if(isset($_GET['joined']))
        {
             ?>
             <div class="alert alert-info">
                  <i class="glyphicon glyphicon-log-in"></i> &nbsp; Successfully registered <a href='index.php'>login</a> here
             </div>
             <?php
        } ?>

            Vardas:<br>
            <input type="text" name="name" id="name" placeholder="Vardas" required>
            <br>
            Pavardė:<br>
            <input type="text" name="surname" id="surname" placeholder="Pavardė" required>
            <br>
            Prisijungimo vardas:<br>
            <input type="text" name="username" id="username" placeholder="Prisijungimo vardas" required>
            <br>
            Slaptažodis:<br>
            <input type="password" name="password" id="password" placeholder="Slaptažodis" required>
            <br>
            El. pašto adresas: <br>
            <input type="email" name="email" id="email" placeholder="El. pašto adresas" required>
            <br><br>
            <div class ="div">
            <input type="submit" name="submit" id="submit" value="Registruotis">
            <br><br>
            <label>Have an account? <a href="index.php">Sign In</a></label>
            </form> 
    </div>
</body>

Thank you for trying to help!

Advertisement

Answer

Your method needs 5 arguments, you only pass 2: User->register('ds', 'dsssssss')

Edit this line in Register.php:

$user->register($username, $password)

to

$user->register($name, $surname, $username, $password, $email)

Additionally you have this line twice $stmt->bindParam(":password", $password);

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement