How can I upload multiple images with php and mysql?

0

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();
}

    
asked by anonfidusa 27.04.2017 в 17:53
source

1 answer

0

Of the tables you mention, I would have only:

products ( producto_id , product_title, product_price, product_description, product_image)

gallery (image_id, producto_id , image_name, image_date)

For your PHP (Idea):

<?php
// Aqui los procesos para conectarte a tu BD (omito el IF)

   // Insertas los datos del producto
   // IMPORTANTE: Obtienes el ID del producto recien agregado
   //             aqui le llamare $producto_id (ya asignado)

  $i=0; // No se tu código o condiciones previas pero normalmente se inicia en cero; el primer elemento (salvo claro se declare/asigne de otra forma) es 0 (cero)

  foreach ($_FILES['imagen_name']['name'] as $f)
  {
     // Asumo esto ya lo tienes probado así esta correcto (no he manejado esta estructura)
     // ¿usar $_FILES es correcto para que crear $f?
     // Esto no lo he usado así solo he realizado con un archivo por vez.
     $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, producto_id,imagen_name,imagen_date) VALUES (null,:prod_id,:imagen_name,CURRENT_TIMESTAMP)'
     );

     $statment->execute(array(
         ':prod_id' => $producto_id,
         ':imagen_name' => $_FILES['imagen_name']['name'][$i]
     ));


     $i++;

   }

// En caso consideres; los procesos para liberar la conexión de la BD (es opcional)
?>

Those who review your approach will be very useful if you include the error that is generated; only knowing fails makes it think that it is on the developer's side or error not activated to show itself; while you are exceeding the detail add (and then remove) error_reporting(E_ALL); at the beginning of the PHP Script or change in the PHP configuration file error_reporting you set it to E_ALL .

    
answered by 27.04.2017 в 20:00