Trying to insert data even if any index of array in foreach loop is empty. trying the following script its giving Undefined index: error in case of empty index.
$data = array();
if(count($_POST['data']) > 0 && !empty ($_POST['data'])){
foreach($_POST['data'] as $key => $array){
$row = array();
$row['team_id'] = intval($array['team_id']);
$row['Note'] = strip_tags(trim(strval($array['Note'])));
$row['result'] = strip_tags(trim(strval($array['result'])));
$data[$key] = $row;
}
$sql = $db->prepare("INSERT INTO teams ('team_id','note','result') values (:team_id, :note, :result) ");
foreach($data as $key => $array){
$sql->execute(array(':team_id' => $array['team_id'], ':note' =>$array['Note'], ':result' => $array['result'], ));
}
}
isset($_POST['data'])andis_array($_POST['data'])first.&& !empty ($_POST['data']).isset($array['index'])first$_POST['data']. Try thisif(!empty ($_POST['data'])){count($_POST['data']) > 0, does it? It can't be empty if its count is bigger than zero.