php - How to insert multiple image into one mysql database row? -


okay briefly whats problem code have. code below uploads image data mysql database beautifully except 1 barrier. on form have, people can upload 4 images. now, code below inserts 4 different rows. each image 1 row. title 4 rows same because belongs same post. there anyway can comma delimited insertion 4 images single post in 1 row?

enter image description here

<?php    if(isset($_post['submitting']))        {       if(isset($_files['file_array']))          {             $user = $_session['user_id'];             $pname = $_post['product_name'];             $proprice = $_post['product_fee'];             $n_array = $_files['files_array']['name'];         $tmp_name_array = $_files['files_array']['tmp_name'];         $type_array = $_files['files_array']['type'];         $size_array = $_files['files_array']['size'];         $error_array = $_files['files_array']['error'];         for($i = 0; $i < count($tmp_name_array); $i++)         {             if(move_uploaded_file($tmp_name_array[$i], "data/profile/posted_data/".$n_array[$i]))             {                 $query = mysqli_query($conn, "insert posts (userid, post_title, file, type, size, image_date) values ('$user', '$pname', '$n_array[$i]', '$type_array[$i]', '$size_array[$i]', now())");                 echo '<div class="form_message_box">' . $n_array[$i] . '   ' . 'uploaded successfully' . '</div>' . '<br>';                 if($query)                                          {                        echo '' . '<br>';                     }                         else                     {                        echo 'failed!' . '<br>';                                         }             }         }     }  } ?>               $posts = "select * posts userid='$user'";         $posts_result = mysqli_query($conn, $posts);         $res = mysqli_num_rows($posts_result);         while ($posts_result_rows = mysqli_fetch_assoc($posts_result)) {                                   $post_image = $posts_result_rows['files'];                             }                              <tr>                 <td width="220">                 <?php                                  <img src="<?php echo "data/profile/posted_data/".$post_image; ?>" width="220" height="220"><hr>                  </td>                     </tr>      <?php             }     echo "</table>";                     ?> 

i think need different schema:

table 1: - userid - id  - post_title  table 2: - id - post_id (fk table 1) - file - type - size - image_date - sort (if want sort images) 

with relationship 1:many can add many images 1 title want , @ same time not have duplicate data.

pros of answer:

  • better database performance , better schema
  • easier sql queries (like inner join, distinct by, etc.)

cons of want achieve right now (if continue logic have right now, want store file name single string single post):

  • slower performance
  • when fetch data single post (e.g. dadashi.jpg, 3.jpg, 2.jpg), have use functions explode() , run them in loop in order display them in <img>.
  • when want add image post, have fetch file , add string of new file name, update table

an example it:

fetch post (i use prepared statement):

if($stmt = $con->prepare("select userid, id, post_title table1")){ /* fetch post query */   $stmt->execute(); /* execute query */   $stmt->store_result(); /* necessary when nesting second statement */   $stmt->bind_result($userid,$id,$post_title); /* bind result these queries */   while($stmt->fetch()){ /* start fetching post results */      ?>       <tr> <!-- start row -->         <td><?php echo $post_title; ?></td> <!-- first column title -->         <td> <!-- second column files attached post -->     <?php      if($stmt2 = $con->prepare("select post_id, file, type, size, image_date, sort table2 id = ?")){ /* prepare query fetch files attached current post */        $stmt2->bind_param("i",$id); /* bind variable query; stands integer */        $stmt2->execute(); /* execute query */        $stmt2->store_result();         $stmt2->bind_result($postid,$file,$type,$size,$imagedate,$sort); /* bind results these variables */        while($stmt2->fetch()){ /* start fetching files current fetched post */          ?>            <img src="data/profile/posted_data/<?php echo $file; ?>" width="220" height="220"><br>          <?php        } /* end of fetching files current fetched post */        $stmt2->close();     } /* end of second prepared statement */     ?>         </td> <-- end of column -->       </tr> <!-- end of row -->     <?php    } /* end of while loop */   $stmt->close(); } /* end of prepared statement */ 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -