Save image array to different columns

Asked

Viewed 606 times

3

Good night.

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

I have the following code to upload 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 the image shows:

inserir a descrição da imagem aqui

What query radius do I have to do for the array values to be saved in different columns type:

inserir a descrição da imagem aqui

If not possible, how can I access or select each of the images separately for presentation on the page? The code I have only allows me to display the first of the images with the same id...

 <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.

Hug Nuno S.

  • 1

    Does not use functions mysql_*, you can do this based on this answer: http://answall.com/questions/101087/como-passo-par%C3%A2metros-dinamicos-numa-preparedstatment, or you can use the PDO.

2 answers

1

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

Since you need to enter this data into the same record, I have created a solution for inserting it at once, so the foreach looping will not make multiple records.

Behold:

   <?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')");

}

?>

Since I did not take the test there may be some kind of error. But the logic is there.

Another solution would be, rescue these images in looping while

see:

   <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

=)

Hug

  • Fabulastico :)) worked wonderfully @Andrei coelho :o) Thank you all for the advice :) hug

  • Tranquil @Nunosilva .... good luck!

0

Eae Nuno, Blza?

Try to use the Implode in its $maxidpro array, there is to understand that there are three more fields in Insert.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.