Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

php - 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:xampphtdocs
egister.php on line 39 and exactly 5 expected in C:xampphtdocsclassesusers.php:22 Stack trace: #0 C:xampphtdocs
egister.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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In php 7 instead of:

    public function register($name, $surname, $username, $password, $email)
{
...

use:

public function register($name = null, $surname = null, $username = null, $password = null, $email = null)
{
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...