Hello,
I have a code that only add an image in my database and I want to add more, but only one form. Can anyone give me some ideas how to do this? thanks

Recommended Answers

All 5 Replies

Hello,
I have a code that only add an image in my database and I want to add more, but only one form. Can anyone give me some ideas how to do this? thanks

Hello,

Assuming you can use multiple inputs in the form you just have to loop your $_FILES array,

1)

<input type="file" name="my_image[]" />
<input type="file" name="my_image[]" />

foreach(array_keys($_FILES['my_image']['name']) as $idx) {
     .... 
}

2)

<input type="file" name="image_1" />
<input type="file" name="image_2" />

for ($x = 1; $x <= 5; $x++) {
     echo "Image is ", $_FILES["image_$x"]['name'];     
}

Feel free to Buzz, cheers !

Hey,Thanks for the reply.helpful to me your answer :)
but nou i have another problem...in the edit profile page where I upload my photos, and display pictures that I've loaded, but if I press submit without replacing pictures, in database insert the empty field ... how can I make my pictures that still remain that I've uploaded before?
Thanks.Dani

Hey,Thanks for the reply.helpful to me your answer :)
but nou i have another problem...in the edit profile page where I upload my photos, and display pictures that I've loaded, but if I press submit without replacing pictures, in database insert the empty field ... how can I make my pictures that still remain that I've uploaded before?
Thanks.Dani

Just put an if condition before you insert. :)

Here is my code:

$file = $_FILES['img_field'];
    $file_name = $_FILES['img_field']['name'];
    $file_tmp_name = $_FILES['img_field']['tmp_name'];    
	
	$file1 = $_FILES['img_field1'];
    $file_name1 = $_FILES['img_field1']['name'];
    $file_tmp_name_1 = $_FILES['img_field1']['tmp_name1'];    

	$file2 = $_FILES['img_field2'];
    $file_name2 = $_FILES['img_field2']['name'];
    $fileTmpName = $_FILES['img_field2']['tmp_name']; 
	
	$file3 = $_FILES['img_field3'];
    $file_name3 = $_FILES['img_field3']['name'];
    $file_tmp_name = $_FILES['img_field3']['tmp_name']; 
	
    $file4 = $_FILES['img_field4'];
    $file_name4 = $_FILES['img_field4']['name'];
    $file_tmp_name = $_FILES['img_field4']['tmp_name'];

$query = mysql_query.....

     $path = "site_images/$file_name1";   
     $path = "site_images/$file_name2";
     $path = "site_images/$file_name3";
     $path = "site_images/$file_name4";
     $path = "site_images/$file_name";
    
    //use move_uploaded_file function to upload or move file to the given folder or path
    if(move_uploaded_file($file_tmp_name,$path)) 
    { 
     
    }
	    else
    {
     echo "";
    } 
	if(move_uploaded_file($file_tmp_name1,$path)) 
    { 
     
    } 
        else
    {
     echo "";
    } 
    if(move_uploaded_file($file_tmp_name2,$path)) 
    { 
     
    } 
        else
    {
     echo "";
    } 
    if(move_uploaded_file($file_tmp_name3,$path)) 
    { 
     
    } 
        else
    {
     echo "";
    } 
	if(move_uploaded_file($file_tmp_name4,$path)) 
    { 
     
    } 
	    else
    {
     echo "";
    } 
	mysql_close($con);

and the problem is that only the first image is upload....waht is wrong?
Thanks for your time !
Dani.

$path = "site_images/$file_name1";
$path = "site_images/$file_name2";
$path = "site_images/$file_name3";
$path = "site_images/$file_name4";
$path = "site_images/$file_name";

use different variables :icon_frown:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.