-1

I have created a form to upload multiple images and text to the database.

The Insert script:

    include 'config.php';
            $prodName=$_POST['prodName'];
            $location="upload/";
            $file1=$_FILES['img1']['name'];
            $file_tmp1=$_FILES['img1']['tmp_name'];
            $file2=$_FILES['img2']['name'];
            $file_tmp2=$_FILES['img2']['tmp_name'];
            $data=[];
            $data=[$file1,$file2];
            $images=implode(' ',$data);
            $prodID = mysqli_real_escape_string($db, $_POST['prodID']);
            $null = null;
            $query="insert into overview (prodID,prod,prodName,imageProd) 
                    values('$prodID','$null','$prodName','$images')";

$fire=mysqli_query($db,$query);
    if($fire)
    {
        move_uploaded_file($file_tmp1, $location.$file1);
        move_uploaded_file($file_tmp2, $location.$file2);
        move_uploaded_file($file_tmp3, $location.$file3);
        move_uploaded_file($file_tmp4, $location.$file4);
        echo "success";
    }
    else
    {
        echo "failed";
    }
}

The images are successfully uploaded and stored into one field as shown here

there are two images: PNG_trans.png and aa.jpg

How do I display/echo these images on my page to make it as a slider/carousel

Pejman Kheyri
  • 4,044
  • 9
  • 32
  • 39
Laan
  • 33
  • 6
  • 6
    You shouldn't be storing multiple values in a single field like that. It breaks the principles of relational database design. It also makes it harder to query the data, collect statistics about it, count it, delete individual items, etc etc. It's just a bad idea. You need a second table called overviewImages (or similar) and should insert each image name into a new row in that table, together with the ID of the overview record it is associated with. This is a pretty standard foreign key relationship (sometimes called one-to-many relationship). – ADyson Mar 12 '21 at 09:14
  • 1
    Don't do that. Don't store serialized datas, you're losing the purpose of using a RDBMS. Think about [Database normalization](https://en.wikipedia.org/wiki/Database_normalization) – Cid Mar 12 '21 at 09:15
  • Anyway your actual question is very broad. Where exactly are you stuck with this process? There are plenty of carousel/slider plugins available. Or are you wanting to make your own? Or is the problem about how to query the data? Or something else? It's unclear. Break the requirement down into the smaller steps of the process you need, and work out how to do each one. If you get stuck on a specific one, give more details of what you've done so far, and what exactly the issue is. – ADyson Mar 12 '21 at 09:16
  • 1
    How can you make the difference between **a single file** named `foo.png bar.jpg` and **2 files** named `foo.png` and `bar.jpg`? – Cid Mar 12 '21 at 09:17
  • Thank you for the information. I am still new in web development and as i referred videos and scripts, I have done it this way. Just for my knowledge can I know the script on how to call out these images and display them?. – Laan Mar 12 '21 at 09:26
  • Regarding the script - refer to my second comment. Your question is too broad. We don't know where you are stuck. Are you asking for a SQL query, or how to generate some HTML, or what? It's really unclear. This site isn't a free write-my-code service. We'll _help_ you if you can identify a more specific programming problem, either to do with some code you've written, or perhaps a _small_ missing piece from a bigger bit of code you've already created. We won't just write a whole feature for you, or find you a tutorial. What **exactly** do you want help with? – ADyson Mar 12 '21 at 09:29
  • `as i referred videos and scripts, I have done it this way`...in that case unfortunately I think you have found some poor-quality tutorials. – ADyson Mar 12 '21 at 09:30
  • Another big issue: Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. A good tutorial would never have shown you to write the code like that. – ADyson Mar 12 '21 at 09:30
  • https://phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped input values. – ADyson Mar 12 '21 at 09:31
  • P.S. And another issue: `move_uploaded_file($file_tmp3, $location.$file3); move_uploaded_file($file_tmp4, $location.$file4);` should be generating warnings because `$file3` and `$file4` don't exist in your code. This whole block of code is inflexible because it doesn't allow for an unpredictable number of files to be uploaded. A loop should be used to process each file found in `$_FILES`, one by one, using the same code each time. Then it doesn't matter how many files are actually uploaded, the code behaves the same. Simple example: https://stackoverflow.com/a/12006515/5947043 – ADyson Mar 12 '21 at 09:33
  • Thank you for the meaningful information. I will refer to the per-mentioned links you suggested. – Laan Mar 15 '21 at 00:25

1 Answers1

0

Assuming you have some reason not to follow well meaning advice above you could use explode to convert your string in imageProd to an array.

Cuervo
  • 21
  • 2