0

I searched Google all over the place for an answer but I can't seem to find the right one so I'll try it here.

I want to store the users' firstname, lastname and the path of the images in the mysqli database and the image in the folder uploads. (the user can upload multiple images)

This works and is no problem.

The issue is:

When for example they type their firstname and lastname and select two images and press the upload button it uploads everything but it also creates two rows with the exact same values.

I want to store everyting in one row, see my code below:

<?php

$con = mysqli_connect('localhost','root','','img_db');
mysqli_select_db($con,'img_db');

$rn = mt_rand();

if(isset($_POST['submit'])&& isset($_FILES['image'])&& !empty($_POST['firstname'])&& !empty($_POST['lastname']))
{
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];

    for($i=0; $i<count($_FILES['image']['name']); $i++)
    {
        $tmp_name = $_FILES['image']['tmp_name'][$i];
        $max_size = 100000;
        $path = "uploads/";  
        $name = $_FILES['image']['name'][$i];
        $size = $_FILES['image']['size'][$i];
        $type = $_FILES['image']['type'][$i];

        $ext = strtolower(substr($name, strpos($name, '.') +1));
        $name = str_replace('_','',trim($name));
        $name = str_replace('-','',trim($name));
        $name = str_replace(' ','',trim($name)); 
        $name = str_replace('__','',trim($name));

        if(($ext=='jpg' || $ext=='jpeg' || $ext=='png' || $ext=='gif')&&($type=='image/jpeg' || $type=='image/png' || $type=='image/gif')&&$size<=$max_size)
        {  

            if(move_uploaded_file($_FILES['image']['tmp_name'][$i], $path.$rn.$name)) 
            { 
                mysqli_query($con,"INSERT into `img` (firstname,lastname,url) VALUES('$firstname','$lastname','$rn$name')");
            }
        }// END EXT

    }// END FOR LOOP

}// END IF ISSET POST SUBMIT

?>
Meetarp
  • 2,331
  • 1
  • 23
  • 31
Tea bag
  • 5
  • 1
  • 6
  • 2
    How did you want to store the multiple file urls in the one field? – Meetarp Dec 16 '14 at 09:26
  • 1
    I don't get it : if the user uploads 2images, your code will upload 2files, and insert 2rows. You want everything in 1row? If so, you should change your database model... – johnkork Dec 16 '14 at 09:26

1 Answers1

1

Suggestion 1

One solution is to store uploaded file urls in an array (declared before the for loop) and utilize the array outside of the for loop, like so:

if (isset(/* ... */)) {
  // Outside your for loop
  $files = array();

  for (/* .. */) {

    // Doing things

    if (/* $ext stuff */) {
      if (move_uploaded_file(/* ... */))
      {
        $files[] = $rn.$name;
      }
    } // End EXT
  } // End FOR

  // Utilize $files array to determine how you want to store all of these URLs in one field.
  // Let's say you created a variable called $store with the desired representation.
  mysqli_query($con,"INSERT into `img` (firstname,lastname,url) VALUES('$firstname','$lastname','$store')");

} // End ISSET

Suggestion 2

You could use a two-table structure (one table is users, one table is images) and create a foreign key relation from each image table row to a user table row via the desired user's primary key.

See: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Meetarp
  • 2,331
  • 1
  • 23
  • 31
  • Ok first off thank you all for the answers, and how would i do this if i had two input file types and two mysqli rows (url1,url2) without using multiple. – Tea bag Dec 16 '14 at 09:53
  • Not sure what you mean by 'multiple', but I think a decent way to go about it is to use a two-table approach. See suggestion 2 in my edited answer. Hopefully you didn't mean "without using multiple tables". – Meetarp Dec 16 '14 at 10:19
  • In the case that you did mean "without using multiple tables", you could just store the urls as a comma-separated list of strings which you can call PHP's explode() on when you retrieve the data. See: http://php.net/explode – Meetarp Dec 17 '14 at 18:23