What I want to do is upload a product (that works for me) and the images of that product in another table in this case, and then save the id of the product and the image and store them in the table productos_galeria.
My tables are:
products
- product_id
- producto_title
- product_price
- producto_description
- producto_image
gallery
- imagen_id
- imagen_name
- imagen_date
products_galeria
- imagen_id
- product_id
My code
if ($_SERVER['REQUEST_METHOD'] == 'POST'){ $categoria_lists = $_POST['categoria_id']; $producto_steps = $_POST['producto_title']; $producto_price = $_POST['producto_price']; $producto_description = $_POST['producto_description']; $producto_image = $_FILES['producto_image']['tmp_name']; $producto_image_upload = '../' . $items_config['images_folder'] . $_FILES['producto_image']['name']; move_uploaded_file($producto_image, $producto_image_upload); $statment = $connect->prepare( 'INSERT INTO productos (producto_id,producto_title,producto_price,producto_description,producto_image) VALUES (null, :producto_title, :producto_price, :producto_description, :producto_image)' ); $statment->execute(array( ':producto_title' => $producto_title, ':producto_price' => $producto_price, ':producto_description' => $producto_description, ':producto_image' => $_FILES['producto_image']['name'] )); $i=1; foreach ($_FILES['imagen_name']['name'] as $f) { $imagen_name = $_FILES['imagen_name']['tmp_name'][$i]; $imagen_name_upload = '../' . $items_config['imagenes_folder'] . $_FILES['imagen_name']['name']; move_uploaded_file($imagen_name, $imagen_name_upload); $statment = $connect->prepare( 'INSERT INTO galeria (imagen_id,imagen_name,imagen_date) VALUES (null,:imagen_name,CURRENT_TIMESTAMP)' ); $statment->execute(array( ':imagen_name' => $_FILES['imagen_name']['name'][$i] )); $i++; } $statment = $connect->prepare("SELECT @@identity AS id"); $statment->execute(); $resultado = $statment->fetchAll(); $id = 0; foreach ($resultado as $row) { $id = $row["id"]; } $statment = $connect->prepare( 'INSERT INTO productos_galeria (imagen_id,producto_id) VALUES (:imagen_id, :producto_id)' ); $statment->bindParam(':imagen_id', $idimagen); $statment->bindParam(':producto_id', $id); foreach ($imagen_lists as $option_value) { $idimagen = $option_value; $statment->execute(); }