当前位置: 首页 > 知识库问答 >
问题:

致命错误:未捕获的ArgumentCountError:参数太少,无法::deletecategory(),

祝宾白
2023-03-14

我知道有一些与此相关的问题,但是有c或其他语言。我得到了这个错误,我不确定我的功能出了什么问题。这是我的错误

致命错误:Uncaught ArgumentCounter错误:函数Admincategory::deletecategory()的参数太少,在F:\xampp\htdocs\digikalamvc\core\app中传递了0。php位于第34行,在F:\xampp\htdocs\digikalamvc\controllers\admincategory中正好是1。php:50堆栈跟踪:#0 F:\xampp\htdocs\digikalamvc\core\app。php(34):Admincategory-

我的职能是:

<?php

class Admincategory extends Controller
{


    function __construct()
    {
        parent::__construct();
    }


    function index()
    {

        $category = $this->model->getChildren(0);
        $data = ['category' => $category];
        $this->view('admin/category/index', $data);
    }


    function showchildren($idcategory)
    {

        $categoryInfo = $this->model->categoryInfo($idcategory);
        $children = $this->model->getChildren($idcategory);
        $parents = $this->model->getParents($idcategory);
        $data = ['categoryInfo' => $categoryInfo, 'category' => $children, 'parents' => $parents];

        $this->view('admin/category/index', $data);
    }


    function addcategory($categoryId = 0, $edit = '')
    {

        if (isset($_POST['title'])) {
            $title = $_POST['title'];
            $parent = $_POST['parent'];
            $this->model->addCategory($title, $parent, $edit, $categoryId);
        }
        $category = $this->model->getCategory();
        $categoryInfo = $this->model->categoryInfo($categoryId);

        $data = ['category' => $category, 'parentId' => $categoryId, 'edit' => $edit, 'categoryInfo' => $categoryInfo];
        $this->view('admin/category/addcategory', $data);
    }


    function deletecategory($parentId)
    {
        $ids = $_POST['id'];
        $this->model->deletecategory($ids);
        header('location:'.URL.'admincategory/showchildren/'.$parentId);
    }

}

?>

应用程序。php文件

<?php


class App
{

    public $controller = 'index';
    public $method = 'index';
    public $params = [];


    function __construct()
    {
        if (isset($_GET['url'])){
            $url = $_GET['url'];
            $url = $this->parseUrl($url);
            $this->controller = $url[0];
            unset($url[0]);
            if (isset($url[1])){
                $this->method = $url[1];
                unset($url[1]);
            }

            $this->params = array_values($url);
        }
        $controllerUrl = 'controllers/' . $this->controller . '.php';
        if (file_exists($controllerUrl)) {
            require($controllerUrl);
            $object = new $this->controller;

            $object->model($this->controller);

            if (method_exists($object, $this->method)) {
                call_user_func_array([$object, $this->method], $this->params);
            }
        }


    }

    function parseUrl($url)
    {
        $url = filter_var($url, FILTER_SANITIZE_URL);
        $url=rtrim($url,'/');
        $url = explode('/', $url);
        return $url;
    }

}



?>

还有index.php

<?php

require('views/admin/layout.php');
$category = $data['category'];

$categoryInfo = [];
if (isset($data['categoryInfo'])) {
    $categoryInfo = $data['categoryInfo'];
}
$parents = [];
if (isset($data['parents'])) {
    $parents = $data['parents'];
    $parents = array_reverse($parents);
}

?>

<style>

    .w400 {
        width: 600px;
    }

</style>

<div class="left">

    <p class="title">
        مدیریت دسته ها

        (
        <?php foreach ($parents as $row) { ?>

            <a href="admincategory/showchildren/<?= $row['id']; ?>">
                <?= $row['title']; ?>
            </a>
            -

        <?php } ?>

        <a href="admincategory/<?php if (isset($categoryInfo['id'])) {
            echo 'showchildren/' . $categoryInfo['id'];
        } else {
            echo 'index';
        } ?>">
            <?php
            if (isset($categoryInfo['title'])) {
                echo $categoryInfo['title'];
            } else {
                echo 'دسته های اصلی';
            }
            ?>
        </a>

        )

    </p>

    <a class="btn_green_small" href="admincategory/addcategory/<?= @$categoryInfo['id']; ?>">
        افزودن
    </a>


    <a class="btn_red_small" onclick="submitForm();">
        حذف
    </a>

    <form action="admincategory/deletecategory/<?= @$categoryInfo['id']; ?>" method="post">

        <table class="list" cellspacing="0">

            <tr>
                <td>
                    ردیف
                </td>

                <td>
                    عنوان دسته
                </td>

                <td>
                    مشاهده زیر دسته ها
                </td>

                <td>
                    ویرایش
                </td>

                <td>
                    انتخاب
                </td>

            </tr>

            <?php

            foreach ($category as $row) {
                ?>

                <tr>

                    <td>
                        <?= $row['id']; ?>
                    </td>

                    <td class="w400">
                        <?= $row['title']; ?>
                    </td>

                    <td>
                        <a href="admincategory/showchildren/<?= $row['id']; ?>">
                            <img src="public/images/view_icon.png" class="view">
                        </a>
                    </td>

                    <td>
                        <a href="admincategory/addcategory/<?= $row['id']; ?>/edit">
                            <img src="public/images/edit_icon.ico" class="view">
                        </a>
                    </td>

                    <td>
                        <input type="checkbox" name="id[]" value="<?= $row['id']; ?>">
                    </td>

                </tr>

            <?php } ?>

        </table>

    </form>

</div>

</div>

============

谢谢你想帮忙!

共有1个答案

袁霍英
2023-03-14

有时,调用AdminCategory::deletecategory($parentId)时没有参数,但prototype没有默认值,因此引发异常。由于您从post请求中获取数据,并且类别始终可能没有父级,因此您可以将方法重构为如下所示:

function deletecategory($parentId = null)
{
    $ids = $_POST['id'];
    $this->model->deletecategory($ids);
    if (null !== $parentId) {
        header('location:'.URL.'admincategory/showchildren/'.$parentId);
    }
    // PUT MORE OF YOUR LOGIC HERE, I DO NOT KNOW WHAT SHOULD HAPPEN
}

若您使用的是键入提示,那个么更合适的方法是使该方法看起来像

 function deletecategory(string $parentId = ''): void //void is for php7.1
 {
    $ids = $_POST['id'];
    $this->model->deletecategory($ids);
    if ('' !== $parentId) {
        header('location:'.URL.'admincategory/showchildren/'.$parentId);
    }
    // AGAIN LOGIC HERE
 }

如果您真的期望必须传递父进程,那么将方法调用者包装为试抓

if (method_exists($object, $this->method)) {
    try {
        call_user_func_array([$object, $this->method], $this->params);
    } catch (\Exception $ex) {
        // HANDLE EXCEPTION HERE
    }
}
 类似资料:
  • 当我的WooCommerce购物车页面为空时,我会在页面上出现此错误,我如何消除此错误消息? 致命错误:未捕获的ArgumentCountError:参数太少,无法执行wc_get_page_id(),0在第30行 /home/s3morder/public_html/wp-content/themes/Intranet主题/伍兹商业/购物车/cart-empty.php中传递,而在 /home/

  • 我得到这个错误。我创建了一个按钮来更新表。当我点击按钮时,我得到一个错误。如何修复它? 致命错误:Uncaught ArgumentCounter错误:函数personel::update_form(),0的参数太少,在C:\xampp\htdocs\warehouse\panel\system\core\CodeIgniter中传递。php在第360行,C:\xampp\htdocs\wareh

  • 我得到以下错误: 致命错误:Uncaught ArgumentCounter错误:参数太少,无法使用wpdb::prepare()函数,第635行的/home/admin/public_html/ocarinatab.com/wp-content/plugins/ocarina-tabs/ocarina-tabs.php中传递了1个,在/home/admin/public_html/ocarina

  • 我做了一个用于训练的MVC框架,我得到了这个错误并且我不确定我的函数出了什么问题。 当我打开如下链接:http://mvctrav.com/posts/show/6时,它会显示文章,但当删除id时,它会显示错误:http://mvctrav.com/posts/show/6 我的错误如下所示: 而我的职能是: 这是我的核心类: 我的问题是:链接http://mvctrav.com/posts/sh

  • 我得到以下错误: 致命错误:Uncaught ArgumentCounter错误:函数wpdb::prepare()的参数太少,1传入/homepages/39/d740877019/htdocs/test/wp content/themes/eduma/formulaire_get。php位于第108行,在/homepages/39/d740877019/htdocs/test/wp inclu

  • 我试图将我的程序登录转换为OOP,但我得到ArgumentCountError。这是我的家庭作业,你能帮我吗? 下面是我的php代码,带有类和登录函数,以及带有小php脚本的HTML登录页面。 数据库连接是PDO。 当我要istance我的php函数与"$login=new Login;$login- 致命错误:Uncaught ArgumentCounter错误:函数Login::Login()