#Dear I have an error when saving this data in my database. This is my first error that appears this shows me the console. Array to string conversion in. All data is array is more than one data
$fecha_anterior_kilometraje_tracto=$_POST['fecha_kilometraje'];
$km_anterior_tracto=$_POST['kilometraje_actual'];
$km_actual_tracto=$_POST['kmactual'];
$fecha_km_tracto=$_POST['fecha_ingreso_km'];
$calculo_km_tracto=$_POST['diferencia_km'];
$stmt = $base->prepare("INSERT INTO detalle_kilometraje('fecha_anterior_kilometraje','km_anterior','fecha_km_actual','km_actual','diferencia_km') VALUES (:fecha_anterior,:km_ant,:fecha_km_actual,:km_actual,:diferencia_km)");
$stmt->bindParam(':fecha_anterior',$fecha_anterior_kilometraje_tracto);
$stmt->bindParam(':km_ant',$km_anterior_tracto);
$stmt->bindParam(':fecha_km_actual',$km_actual_tracto);
$stmt->bindParam(':km_actual',$fecha_km_tracto);
$stmt->bindParam(':diferencia_km',$calculo_km_tracto);
foreach ($_POST['fecha_kilometraje'] as $fecha_anterior_kilometraje_tracto) {
$stmt->execute();
}
If all the elements are arrays and they all have the same number of elements and they are correctly related by their position in the arrays , then try this:
Explanation
prepare
before the loop, since we don't need to put it inside each time.so that in this way, it accesses each element of each array through its index number, represented by $i, and so on in each cycle, until the end.
IMPORTANT Note : You have to be very sure that all the POST arrays always contain the same elements and that they all always correspond to the same index number . Otherwise, execution or insertion errors could easily occur and everything would have to be recoded in another way, including the form from which it came.