我在循环中有一个插入查询,如何检查操作是否已完成或成功?
foreach ($_POST['players'] as $value) {
mysqli_query($conn, "INSERT INTO contactphone(player_id, player_number) VALUES('$value['player_id']','$value['player_number']')");
}
通常对于单个插入,我会mysqli_insert_id
,但在这种情况下我不确定。
答案 0 :(得分:0)
您可以将查询执行行包装在if语句中。我还添加了一个错误检查行,如果查询没有成功执行,它会回显当前实例的错误消息。
foreach ($_POST['players'] as $value) {
if(mysqli_query($conn, "INSERT INTO contactphone(player_id, player_number) VALUES('$value['player_id']','$value['player_number']')") === false){
echo 'error.. The error is '. mysqli_error($conn);
}
}