MKD
MKD

Reputation: 383

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:\xampp\htdocs\register.php on line 39 and exactly 5 expected in C:\xampp\htdocs\classes\users.php:22 Stack trace: #0 C:\xampp\htdocs\register.php(39): User->register('ds', 'dsssssss') #1 {main} thrown in C:\xampp\htdocs\classes\users.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!

Upvotes: 15

Views: 186326

Answers (7)

Developer
Developer

Reputation: 1672

If anyone facing this issue in XAMPP at running Pear commands, then the problem can be found in the file pearcmd.php at line 446 within the function error_handler parameters.

function error_handler($errno, $errmsg, $file, $line, $vars)

A straightforward solution is to provide default value of null to $vars parameter like this

function error_handler($errno, $errmsg, $file, $line, $vars = null)

It will resolve the issue.

Upvotes: 0

Bruce
Bruce

Reputation: 66

In case anyone finds this issue with PHP 8 randomly throwing errors like these (even when using the functions correctly) :

PHP Fatal error: Uncaught ArgumentCountError: time() expects exactly 0 arguments, 1 given in blablabla:15

(that was calling the date function without a second argument) or

PHP Fatal error: Uncaught ArgumentCountError: is_dir() expects exactly 1 argument, 0 given in blablabla:15

(and that was using is_dir with exactly a 1 non-null argument)

In some cases seems to be caused by Zend Opcache. Might be a bug, so try disabling it :

Check (e.g. for fedora/centos with remi packages)

for zts-php

/etc/php-zts.d/10-opcache.ini

just php

/etc/php.d/10-opcache.ini (regular php)

and change

opcache.enable_cli = 0

for cli usage, and/or

opcache.enable = 0

I was working (zts-php CLI) with code included within a closure, and got really weird behaviour executing it right after i changed any code included there. The following executions worked fine, generating lots of confusion !

After disabling opcache, everything works as normal, every single time.

Upvotes: 0

James Kimani
James Kimani

Reputation: 11

Encountered the same issue. Turns out the hosting company had updated the php version. i just added another parameter to the array and gave it a value of null

Upvotes: 1

Acer
Acer

Reputation: 85

For anyone who encountered this error or something similar, the answer below works! I encountered this error when trying to get an older version of WordPress WooCommerce to run on PHP 7.2. Lol, I know. The WooCommerce Product Edit Screen was blank with the error below (which broke the product tabs)

Fatal error: Uncaught ArgumentCountError: Too few arguments to function product_custom_tab::product_tab_options(), 0 passed in wp-includes/class-wp-hook.php on line 286 and exactly 1 expected in /wp-content/plugins/woo-product-tab/includes/product_custom_tab.php:64 Stack trace: #0 wp-includes/class-wp-hook.php(286):

When going to line 64 of product_custom_tab.php. I changed

public function product_tab_options($product_type)

to

public function product_tab_options($product_type = null)

And it worked! Thanks to the contributors below! You really made my day. Thanks for being here. Thank you!

Bytw: I tried to post this as a comment, but it wouldn't let me, so I posted this as an answer instead.

Upvotes: 4

Ben.12
Ben.12

Reputation: 784

I experienced this same error after my hosting company updated our PHP version from 7.0.x to 7.1.x. They assumed that since it was a minor update, it should be compatible with previous versions. They were wrong: here's a list of the incompatibilities from the migration page.

In this particular case code that would previously throw a warning about the lack of parameters for a function call is now throwing the fatal error in the OP's question.

The solution is obviously to provide the appropriate parameters as the other answers have indicated.

Upvotes: 8

jtmielczarek
jtmielczarek

Reputation: 598

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

Upvotes: 34

modsfabio
modsfabio

Reputation: 1147

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);

Upvotes: 11

Related Questions