Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中出现的修剪错误_Php_Html - Fatal编程技术网

PHP中出现的修剪错误

PHP中出现的修剪错误,php,html,Php,Html,我正在设计一个应用程序,在我的模式中遇到了一个奇怪的错误 <b>Warning</b>: trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\gurukul\demo2\controller\routemgmt\route_mgmt.php</b> on line <b>7</b><br /> 61 警告:tri

我正在设计一个应用程序,在我的模式中遇到了一个奇怪的错误

<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\gurukul\demo2\controller\routemgmt\route_mgmt.php</b> on line <b>7</b><br />
61
警告:trim()希望参数1是字符串,数组在第7行的C:\xampp\htdocs\gurukul\demo2\controller\routemgmt\route_mgmt.php中给出
61
据我所知,它传递的是数组而不是trim中的字符串。以下是显示错误所在的模式:

<?php
include_once dirname(dirname(dirname(__FILE__))) . "/const.php";
include_once PHP_PATH . "/config1.php";
include_once CONFIG_PATH.'/modal/routemgmt/route_mgmt.php';

function sanitize($input) {
    return htmlspecialchars(trim($input));
}

// Sanitize all the incoming data
$sanitized = array_map('sanitize', $_POST);
$reason = $sanitized['reason'];

if($reason == "insert"){
    $staffs = [];
    $stops = [];
    $name = $sanitized['rname'];
    $code = $sanitized['rcode'];
    $desc = $sanitized['rdesc'];
    $vnum = $sanitized['vnum'];
    $stf = $_POST['staff'];
    $st = isset($_POST['stops'])? $_POST['stops']: [];
    $st = [];
//    foreach($staffs as $staff){
//        $stf[] = array_map('sanitize', $staff);
//    }
//    if(isset($stops)){
//        foreach($stops as $stop){
//            $st[] = array_map('sanitize', $stop);
//        }
//    }

    $val = insertRoute($conn,$name, $code, $desc, $vnum, $stf, $stops);
    echo $val;
}

if($reason == "view"){
    $id = $sanitized['id'];
    $val = [];

    $val = viewRoute($conn,$id);
    echo json_encode($val);
}

if($reason == "edit"){
    $stf = [];
    $stp = [];
    $id = $sanitized['pkid'];
    $name = $sanitized['rname'];
    $code = $sanitized['rcode'];
    $desc = $sanitized['rdesc'];
    $vnum = $sanitized['vnum'];
    $estaffs = $_POST['estaff'];
    $estops = $_POST['estops'];
    $edel = $_POST['del'];

    foreach($estaffs as $val){
        $stf[] = array_map('sanitize', $val);
    }
    foreach($estops as $val){
        $stp[] = array_map('sanitize', $val);
    }
    $cnt = 0;$n_stp = [];
    for($i = 0; $i<sizeof($stp); $i++){
        if($stp[$i]['stat'] != "Exist"){
            $n_stp[$cnt] = $stp[$i];
            $cnt++;
        }
    }

    $val = editValues($conn,$id, $name, $code, $desc, $vnum, $stf, $n_stp, $edel);
    echo $val;
}

if($reason == "delRoute"){
    $id = $sanitized['id'];
    $val = delRoute($conn,$id);
    echo $val;
}

您的
$\u POST
变量可能包含某种数组。通过检查
sanitize
函数中
var\u dump($input)
的输出,找出您发布的内容,或者将其更改为:

function sanitize($input) {
    return htmlspecialchars(trim((string) $input));
}

如果您只是想让它工作。

您可以将“消毒”功能重写为:

function sanitize($input) {
    if (is_array($input))
         return array_map('sanitize', $input);
    else
        return htmlspecialchars(trim($input));
}

这样,它将处理作为数组传递给它的值。

什么是
$estafs=$\u POST['estaff']?我认为它可能是多维数组。我猜您的
$\u帖子中有一个数组,
或类似的数组。顺便说一下:您使用的
htmlspecialchars()
完全错误。它被设计用于安全地输出要包含在HTML中的内容,而不是设计用于输入。错误可能是“数组到字符串的转换”。这一点很明显,因为错误是
trim(),它希望参数1是字符串,数组在
Sure中给出。那你为什么要这样回答?用另一个错误替换一个错误有什么用?我告诉他错误是什么,把
var\u dump
放在哪里,以及如何让他的代码再次解析而不出错。。还有什么要添加的?我可以添加项目,但无法编辑sameI。对不起,我不知道你的意思。你能更新这个问题吗?我的意思是,如果我按照你的建议更改功能,错误就会消失,但我无法编辑条目,因为“保存”按钮将被禁用。嗨,Jigna,我不知道你在说什么“保存”按钮。也许你需要问一个新问题。但与此同时,如果这解决了你的问题,你最好能把它标记为接受。