Hi I am working on a project, and i am with days stuck in this little problem...
I have two tables, "Bosses" and "Events" . One Event can have one or more Bosses.
So what i want is inserting a new Event, and insert all the bosses that i choose. Actually is only inserting the last that i am selecting.
<?php
include "../paginas/conexion.php";
$nombr_reg = $_POST['nombr_reg'];
$descr_reg = $_POST['descr_reg'];
$fecre_reg = $_POST['fecre_reg'];
$statu_reg = 1;
$ident_ref = 1;
$sqlInsert = "
INSERT INTO tab_reg(nombr_reg,descr_reg,fecre_reg,statu_reg)
VALUES ('$nombr_reg', '$descr_reg', '$fecre_reg', '$statu_reg')";
mysqli_query($conexion, $sqlInsert);
$lastInsertId = mysqli_insert_id($conexion);
for ($i = 0; $i <= count($_POST['ident_jef']); $i++) {
$jefes = array($_POST['ident_jef']);
$sqlInsertItem = "
INSERT INTO det_reg(ident_reg, ident_jef)
VALUES ('$lastInsertId', '$jefes[$i]')";
mysqli_query($conexion, $sqlInsertItem);
}
print_r($sqlInsert);
print_r($sqlInsertItem);
print_r($jefes);
//header('location: ../paginas/registro_registro_exito.php');
?>
And this is what te print_r();
's are showing:
INSERT INTO tab_reg(nombr_reg,descr_reg,fecre_reg,statu_reg) VALUES ('PRUEBA 123', 'PRUEBA 123', '2019-12-25', '1') INSERT INTO det_reg(ident_reg, ident_jef) VALUES ('122', '')Array ( [0] => 3 )
I know that the problem is with the for, or maybe i have to use a foreach, but i am really confused.
Thanks to everyone and Happy New Year!
I think you have problem in assign array to a new array in for
loop. Try this:
...
$stmt = mysqli_prepare($conexion, "INSERT INTO det_reg(ident_reg, ident_jef)
VALUES (?, ?)");
for ($i = 0; $i < count($_POST['ident_jef']); $i++) { //you have an array here
$jefes = $_POST['ident_jef'];
mysqli_stmt_bind_param($stmt, 'ii', $lastInsertId, $jefes[$i]);
mysqli_stmt_execute($stmt);
}
Here is the test example:
$array=[
"Title 1",
"Title 2",
"Title 3"
];
$desc = "Description";
$stmt = mysqli_prepare($conn, "INSERT INTO table1(title, description) VALUES (?, ?)");
for ($i = 0; $i < count($array); $i++) { //you have an array here
$jefes = $array;
var_dump("Test" . $i);
mysqli_stmt_bind_param($stmt, 'ss', $jefes[$i], $desc);
mysqli_stmt_execute($stmt);
}
Mysql Table 1:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.