There is the usual form of adding photos to the server:

<form method="post" multipart="" enctype="multipart/form-data"> <input type="file" name="img[]" multiple> <input type="submit"> </form> 

PHP form processing:

 $img = $_FILES['img']; if(!empty($img)) { $img_desc = reArrayFiles($img); print_r($img_desc); foreach($img_desc as $val) { $newname = date('YmdHis',time()).mt_rand().'.jpg'; move_uploaded_file($val['tmp_name'],'./filedat/'.$newname); } } function reArrayFiles($file) { $file_ary = array(); $file_count = count($file['name']); $file_key = array_keys($file); for($i=0;$i<$file_count;$i++) { foreach($file_key as $val) { $file_ary[$i][$val] = $file[$val][$i]; } } return $file_ary; } 

This code works the way I need it. Uploads multiple images to the server. But I also need to do this so that the links to these files are saved to the database, so that later they can be displayed on the advertisement page. Please show me an example of how to save the loaded images in Mysql into one cell in this way:

img1, img2, img3, etc.

    2 answers 2

    To put all the links in one cell, you need to enter a variable (for example, $ links) in this part of your code, which will contain all these links separated by commas:

     foreach($img_desc as $val) { $newname = date('YmdHis',time()).mt_rand().'.jpg'; move_uploaded_file($val['tmp_name'],'./filedat/'.$newname); $links .= './filedat/'.$newname.', '; } 

    Links, of course, you can specify without a folder filedat or, on the contrary, complete - at your discretion. Well, and then create a table in the database where the data will be entered, connect to the database and insert the data. Somehow, for example:

     $connect = mysqli_connect('localhost', 'root', ''); mysqli_select_db($connect, 'mydb'); $query = "INSERT INTO pics (links) VALUES ('$links')"; if($links) { mysqli_query($connect, $query); } 
    • Human thanks! Thank you - Maqsood

    Usually, if you need to bind an arbitrary number of objects to any record in the database, be it links to files, a list of parameters, a set of user roles or something else, then for these entities use a separate table with a key binding to the main one.

     CREATE TABLE main ( id VARCHAR(20) NOT NULL PRIMARY KEY, ....., PRIMARY KEY (id) ) CREATE TABLE pictures( id INT, parent_id VARCHAR(20), path VARCHAR(500), INDEX parent (parent_id), FOREIGN KEY (parent_id) REFERENCES main(id) ON DELETE CASCADE ) 
    • 2
      As a supplement, I note that it is desirable to store images not in the database, but on the disk, and only links / information about them should be recorded in the database. - Miron
    • Thanks for attention! - Maqsood
    • @Miron, the right opinion! - Maqsood