0

I'm trying to to append images references to columns image1-4 in my database, instead I'm getting the images uploaded x3 times each and with random references in phpmyadmin.

<?php

$user_name = "root";
$user_pass = "";
$host_name = "localhost";
$db_name = "melony";

$con = mysqli_connect($host_name, $user_name, $user_pass, $db_name);


if($con)
{

    $image = $_POST['image'];
    $name = $_POST['name'];
    $lat = (float) $_POST['lat'];
    $lng = (float) $_POST['lng'];


    for($i=1; $i<5; $i=$i+1){
        $now = DateTime::createFromFormat('U.u', microtime(true));
        $id = $now->format('YmdHisu');
        $upload_path = "uploads/$id.jpeg";


        $sql = "insert into uploads(name,lat,lng,image,image2,image3,image4) values 
                ('$name','$lat','$lng','$upload_path','a','a','a')ON DUPLICATE KEY UPDATE 
                image$i = '$upload_path'";

        if(mysqli_query($con,$sql))
        {
            file_put_contents($upload_path,base64_decode($image));
            echo json_encode(array('response'=>'Image Upload Successfully'));
        }
        else
        {
            echo json_encode(array('response'=>'Image upload failed1,    

            mysqli_error($con)'));
        }
    }
}
else
{
    echo json_encode(array('response'=>'Image Upload Failed2'));
}

mysqli_close($con);

?>
icecub
  • 8,615
  • 6
  • 41
  • 70
Andri
  • 43
  • 9
  • 1
    why there is so many Z in your post – Arpit Solanki Jun 11 '17 at 19:00
  • filling space due to required code/description ratio – Andri Jun 11 '17 at 19:02
  • Your insert should be after the loop. Gather the info in the loop for each image, then do the insert just once. – Sloan Thrasher Jun 11 '17 at 19:10
  • @SloanThrasher He shouldn't be doing it like this at all. Aside from being wide open to SQL injection, any visitor could just upload a PHP script and do some very bad stuff with his server.. – icecub Jun 11 '17 at 19:12
  • Have a look at my answer over here: https://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921 It will guide you step by step on how to create a secure image upload script. – icecub Jun 11 '17 at 19:15
  • @Andri I was working on my next comment (as you can see) where it tells you how you should do it. Relax. No one is picking on you here :) – icecub Jun 11 '17 at 19:16
  • Sorry, that's why I deleted mine, I'll read through carefully what you had posted. Thank you, since I'll surely need to add security anyhow. – Andri Jun 11 '17 at 19:20
  • Great. You should easily be able to follow along with that guide. But if you have any questions or need some help tailoring it to what you need, feel free to leave me a comment and I'll be happy to help you out. – icecub Jun 11 '17 at 19:24

0 Answers0