Save array of images in different columns

3

Good evening.

I would like to count on your help in solving a problem that has been grinding my molecule for a few days and I still can not find a solution.

I have the following code to send to the 4-photo server:

if(isset($_POST['submit']))
											{
$selectmaxpro=mysql_fetch_array(mysql_query("select max('imp_id') as 'impid' from 'imp'"));
$maxidpro=$selectmaxpro['impid']+1;

$path = "imo/uploads/"; 
$count = 0;
foreach ($_FILES['files']['name'] as $f => $name) {


	mysql_query("insert into 'images_imp' values('','$name','$maxidpro')");

	      
	            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) {
	            	$count++;
	            }


	}										
		

So far so good ... my problem is that in the database the elements are all followed assuming the same id. How to display the image:

WhatquerystringdoIhavetodosothatarrayvaluesarewrittentodifferentcolumnstype:

Ifitisnotpossible,howcanIaccessorselecteachoftheimagesinisolationforpresentationonthepage?ThecodeIhavejustallowsmetodisplaythefirstoftheimageswiththesameid...

 <div id="imagem1">

	<?php
				
 $selectphoto=mysql_fetch_array(mysql_query("select * from 'images_imp' where 'imp_id'='$id' "));
				 
	?>
		
  <img id="targetLayer" src="imo/uploads/<?php echo $selectphoto['imag']; ?>" alt="Image">

		
  </div>

Thanks in advance for your help.

Hugs Nuno S.

    
asked by anonymous 11.12.2015 / 05:11

2 answers

1

Well, the little I've seen of this architecture, I found a simpler solution for you to get what you need.

As you need to insert this data into the same record, I have created a solution for one-time insertion, so looping foreach will not make multiple records.

See:

   <?php 

   if(isset($_POST['submit']))
                                        {
    $selectmaxpro=mysql_fetch_array(mysql_query("select max('imp_id') as 'impid' from 'imp'"));
    $maxidpro=$selectmaxpro['impid']+1;

    $path = "imo/uploads/"; 
    $count = 0;

    // vamos criar um array das imagens
    $imagens = array();

    foreach ($_FILES['files']['name'] as $f => $name) {

            // a inserção será feita uma vez só, então retirei o a query de dentro fo foreach

            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) {

                // inseri a imagem no array... e isso sera feito sempre até acabar...
                array_push($imagens, $name);

                $count++;
            }


    }


    // e ai insiro as imagens de uma vez só no mesmo registro...

    $img1 = $imagens[0];
    $img2 = $imagens[1];
    $img3 = $imagens[2];
    $img4 = $imagens[3];


 mysql_query("insert into 'images_imp' values('','$img1', '$img2', '$img3', '$img4', '$maxidpro')");

}

? >

As I have not done the test there may be some kind of error. But the logic is there.

Another solution would be to retrieve these images while looping while

view:

   <div id="imagem1">

<?php

 $selectphoto=mysql_fetch_array(mysql_query("select * from 'images_imp' where 'imp_id'='$id' "));


   // com apenas este código ele mostrará todas as imagens que estão com o mesmo 'imp_id'
while($selectphoto){

?>

 <img id="targetLayer" src="imo/uploads/<?php echo $selectphoto['imag']; ?>" alt="Image">


<?php 

// não esqueça de fechar o looping =)

}

?>

As mentioned in the comments, stop using mysql_query. Use mysqli or PDO

=)

Embrace

    
11.12.2015 / 16:57
0

Eae Nuno, Blza?

Try to use Implode in your $ maxidpro array, then you can understand that there are three more fields in the insert.

    
11.12.2015 / 11:24