在PHP中出现修剪错误

时间:2018-03-30 20:06:28

标签: 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

据我所知,它在修剪中传递数组而不是字符串。下面是显示错误的模态:

<?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;
}

有人可以指导我如何解决这个问题?尝试了一些调试步骤,但没有成功

2 个答案:

答案 0 :(得分:1)

您可以将您的清理功能重写为:

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

这样它就会处理传递给它的值,这是一个数组。

答案 1 :(得分:0)

您的public class mainclass { public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } @Override public String toString() { return "mainclass{" + "name=" + name + ", sname=" + sname + '}'; } String name; String sname; } 变量可能包含某种数组。通过检查$_POST函数中var_dump($input)的输出或将其更改为此来确定您发布的内容:

sanitize

如果你只是想让它发挥作用。