如何通过POST将动态数组传递给php脚本

时间:2019-11-13 10:47:54

标签: flutter

我想通过邮政将整个动态数组传递给php脚本。这是我的代码

final items = ["1", "2", "3"];// Its dynamic with different length depends on the user input

_save() async{
    final response = await http.post(
        "http://myip/Flutter/insert.php",
        body:json.encode({
            "arraydata" :items.toString(),
        }
    ));

    print(response.body);
}

在insert.php中,我的代码是

$array = $_POST['arraydata'];

foreach( $array as $key ) {

$result = $connect->query("UPDATE users SET post='A' WHERE username = '$key'");

if($result){
    echo "success";
} else {
    echo "error";}
}

出现以下错误

Undefined index: arraydata
Invalid argument supplied for foreach()

请帮助我...

1 个答案:

答案 0 :(得分:0)

不要调用json.encode(),否则您可能必须在PHP中调用json_decode($_POST),但是我不确定这是否行得通。 像这样称呼它:

final response =await http.post("http://myip/Flutter/insert.php", body( {...} ))