Updating 3 or any one of existing 3 images in a product page

Dear all,
Please can any one help,

I am writing a code for updating one or all the 3 images in my product page but the query was not running, The below is the code I am trying to work on and I think is the line one that is causing the issue but I don’t know what to do.

 if(is_uploaded_file($_FILES['file']['tmp_name'])){

            // work for upload / update image
        
        $product_img1 = $_FILES['product_img1']['name'];
        $product_img2 = $_FILES['product_img2']['name'];
        $product_img3 = $_FILES['product_img3']['name'];
        
        $temp_name1 = $_FILES['product_img1']['tmp_name'];
        $temp_name2 = $_FILES['product_img2']['tmp_name'];
        $temp_name3 = $_FILES['product_img3']['tmp_name'];
        
        move_uploaded_file($temp_name1,"product_images/$product_img1");
        move_uploaded_file($temp_name2,"product_images/$product_img2");
        move_uploaded_file($temp_name3,"product_images/$product_img3");
        
        $update_product = "update products set p_cat_id='$product_cat',cat_id='$cat',manufacturer_id='$manufacturer_id',date=NOW(),product_title='$product_title',product_url='$product_url',product_img1='$product_img1',product_img2='$product_img2',product_img3='$product_img3',product_price='$product_price',product_keywords='$product_keywords',product_desc='$product_desc',product_sale='$product_sale',product_label='$product_label',product_features='$product_features',product_video='$product_video' where product_id='$p_id'";
        
        $run_product = mysqli_query($con,$update_product);
        
        if($run_product){
            
        echo "<script>alert('Your product has been updated Successfully')</script>"; 
            
        echo "<script>window.open('index.php?view_products','_self')</script>"; 
            
        }
        
    }else{

why do you use $_FILES['file'] and later use $_FILES['product_img1']?

I copy it from Youtube.
I think the person want to upload three images used the HTML input type = file, and the later used HTML name = product_img1, product_img2, product_img3.

I know if it is just one image that I wanted to change I would have used $_FILES[‘product_img1’], or is there anyway I can put the three Images name together on line 1?

but if you use product_img1 as the inputs name there wouldnt be any $_FILES['file'], have a look at print_r($_FILES) after your upload. Read the manual:

https://www.php.net/manual/en/features.file-upload.post-method.php

Thanks, , the below code is what I used when uploading the three images and it works perfectly
$product_img1 = $_FILES[‘product_img1’][‘name’];

$product_img2 = $_FILES['product_img2']['name'];

$product_img3 = $_FILES['product_img3']['name'];



$temp_name1 = $_FILES['product_img1']['tmp_name'];

$temp_name2 = $_FILES['product_img2']['tmp_name'];

$temp_name3 = $_FILES['product_img3']['tmp_name'];



move_uploaded_file($temp_name1,"product_images/$product_img1");

move_uploaded_file($temp_name2,"product_images/$product_img2");

move_uploaded_file($temp_name3,"product_images/$product_img3");



$insert_product = "insert into products (product_cat,cat_id,size,manufacturer_id,date,product_title,product_url,product_img1,product_img2,product_img3,product_price,product_keywords,product_desc,product_features,product_video,seo_keywords,product_label,product_sale) values ('$product_cat','$cat','$size','$manufacturer_id',NOW(),'$product_title','$product_url','$product_img1','$product_img2','$product_img3','$product_price','$product_keywords','$product_desc','$product_features','$product_video', '$product_seo','$product_label','$product_sale')";

Now I want to edit, so I used if statement in case if I don;t want to edit image or if I am editing images and don’t want to tampered with the other text files now what should I put in the first line now if I to be editing the images here is the code again

if(is_uploaded_file(“what should I put here now if I just want edit any or all of the images” )){

        // work for upload / update image

    

    $product_img1 = $_FILES['product_img1']['name'];

    $product_img2 = $_FILES['product_img2']['name'];

    $product_img3 = $_FILES['product_img3']['name'];

    

    $temp_name1 = $_FILES['product_img1']['tmp_name'];

    $temp_name2 = $_FILES['product_img2']['tmp_name'];

    $temp_name3 = $_FILES['product_img3']['tmp_name'];

    

    move_uploaded_file($temp_name1,"product_images/$product_img1");

    move_uploaded_file($temp_name2,"product_images/$product_img2");

    move_uploaded_file($temp_name3,"product_images/$product_img3");

    

    $update_product = "update products set p_cat_id='$product_cat',cat_id='$cat',manufacturer_id='$manufacturer_id',date=NOW(),product_title='$product_title',product_url='$product_url',product_img1='$product_img1',product_img2='$product_img2',product_img3='$product_img3',product_price='$product_price',product_keywords='$product_keywords',product_desc='$product_desc',product_sale='$product_sale',product_label='$product_label',product_features='$product_features',product_video='$product_video' where product_id='$p_id'";

    

    $run_product = mysqli_query($con,$update_product);

    

    if($run_product){

        

    echo "<script>alert('Your product has been updated Successfully')</script>"; 

        

    echo "<script>window.open('index.php?view_products','_self')</script>"; 

        

    }

    

}else{

    // work when no update image

    

    $update_product = "update products set p_cat_id='$product_cat',cat_id='$cat',manufacturer_id='$manufacturer_id',date=NOW(),product_title='$product_title',product_url='$product_url',product_price='$product_price',product_keywords='$product_keywords',product_desc='$product_desc',product_sale='$product_sale',product_label='$product_label',product_features='$product_features',product_video='$product_video' where product_id='$p_id'";

    

    $run_product = mysqli_query($con,$update_product);

    

    if($run_product){

        

    echo "<script>alert('Your product has been updated Successfully')</script>"; 

        

    echo "<script>window.open('index.php?view_products','_self')</script>"; 

        

    }

}

}

Sponsor our Newsletter | Privacy Policy | Terms of Service