php 致命错误:未捕获的 ArgumentCountError:无法运行的参数太少
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43521468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Uncaught ArgumentCountError: Too few arguments to function
提问by MKD
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.
我知道有一些与此相关的问题,但是在 C++ 或其他语言中存在。我收到此错误,但不确定我的函数有什么问题。
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:
注册.php文件:
<!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> <?php echo $error; ?>
</div>
<?php
}
}
else if(isset($_GET['joined']))
{
?>
<div class="alert alert-info">
<i class="glyphicon glyphicon-log-in"></i> 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!
感谢您尝试提供帮助!
采纳答案by modsfabio
Your method needs 5 arguments, you only pass 2: User->register('ds', 'dsssssss')
您的方法需要 5 个参数,您只需传递 2 个: User->register('ds', 'dsssssss')
Edit this line in Register.php
:
编辑此行Register.php
:
$user->register($username, $password)
to
到
$user->register($name, $surname, $username, $password, $email)
Additionally you have this line twice $stmt->bindParam(":password", $password);
另外你有这行两次 $stmt->bindParam(":password", $password);
回答by jtmielczarek
In php 7 instead of:
在 php 7 中,而不是:
public function register($name, $surname, $username, $password, $email)
{
...
use:
用:
public function register($name = null, $surname = null, $username = null, $password = null, $email = null)
{
...
回答by Ben.12
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'sa list of the incompatibilities from the migration page.
在我的托管公司将我们的 PHP 版本从 7.0.x 更新到 7.1.x 后,我遇到了同样的错误。他们认为由于这是一个小更新,它应该与以前的版本兼容。他们错了:这是迁移页面中的不兼容性列表。
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.
在这种特殊情况下,以前会抛出有关函数调用缺少参数的警告的代码现在在 OP 的问题中抛出致命错误。
The solution is obviously to provide the appropriate parameters as the other answers have indicated.
解决方案显然是提供适当的参数,如其他答案所示。
回答by user2381937
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)
对于遇到此错误或类似错误的任何人,以下答案有效!我在尝试让旧版本的 WordPress WooCommerce 在 PHP 7.2 上运行时遇到了这个错误。大声笑,我知道。WooCommerce 产品编辑屏幕为空白,出现以下错误(破坏了产品标签)
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
转到 product_custom_tab.php 的第 64 行时。我变了
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.
Bytw:我试图将此作为评论发布,但它不会让我这样做,所以我将其发布为答案。