如何修复以下错误Message: Invalid argument supplied for foreach() - Line Number: 28
foreach
?为foreach提供的参数无效()
<?php
$mileage = array();
$mileage_input = $this->input->post('mileage');
foreach ($mileage_input as $idx => $name) { //Line 28
$mileage[] = array(
'mileage' => $mileage_input[$idx]
);
}
$data = array(
'mileage' => json_encode($mileage),
'customer_number' => $customer_number,
'name' => $this->input->post('name')
);
$this->db->insert('customer', $data);
?>
尝试'var_dump($ mileage_input)'看看你会得到什么。如果你没有得到一个数组,那么它将无法工作。 –
“里程”字段必须在html表单中命名为“mileage []”,例如''input type =“text”name =“mileage []”...>',否则PHP将不会自动将其转换为_POST/_GET数组中的数组。 –
[为foreach()提供的无效参数的可能的重复)(http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) –