Mukii kumar Mukii kumar - 4 months ago 63
PHP Question

In multiple files upload(PHP/Mysql), the images are stored in single column. Want to store each image in single row with same id

table1

right now the images are stored in single column. I want to store each image in single row.


eg. the id "1149" has two images, now i want the image 1136524631.png in next row with same id i.e 1149. Similar for the id "1150".


here is the code->

if ( isset( $_FILES[ 'image' ][ 'name' ] ) ) {
// Count # of uploaded files in array
$total = count( $_FILES[ 'image' ][ 'name' ] );

$image_name = array();
// Loop through each file
for ( $i = 0; $i < $total; $i++ ) {
//Get the temp file path
$tmpFilePath = $_FILES[ 'image' ][ 'tmp_name' ][ $i ];
$i_names = $_FILES[ 'image' ][ 'name' ][ $i ];
$i_sizes = $_FILES[ 'image' ][ 'size' ][ $i ];
$i_errors = $_FILES[ 'image' ][ 'error' ][ $i ];
$i_tmp_names = $_FILES[ 'image' ][ 'tmp_name' ][ $i ];
$i_types = $_FILES[ 'image' ][ 'type' ][ $i ];
$exts = pathinfo( $i_names, PATHINFO_EXTENSION );
$msgs = '';

if ( $i_errors == 0 ) {
if ( $exts == 'jpg' || $exts == 'jpeg' || $exts == 'png' || $exts == 'gif' ) {
if ( $i_sizes > 0 ) {
$image_name[$i] = rand() . '.' . $exts;
$paths = '../img' . $image_name[$i];
$uploads = copy( $i_tmp_names, $paths );
if ( $uploads ) {
$image_name[$i] = $image_name[$i];
} else {
$image_name[$i] = '';
}
} else {
$image_name[$i] = '';
}
} else {
$image_name[$i] = '';
}
} else {
$image_name[$i] = '';
}
}
}



$image_name = implode(", ", $image_name);

mysql_query( "insert into posts(user_id,title,email,description,`condition`,contact_info,looking_for,price,location,image,is_active,created) values('$user_id','$title','$email','$description','$condition','$contact_info','$looking_for','$price','$location','$image_name','1','$created_date')" );

Answer

ANSWER: I solved the query. Please see the below code snippet

if ( $uploads ) {
                 //insert data into the database
                 $image_id = mysql_query( "insert into post_images(post_id,name) value('$postID','$image_name')" );
                        } else {
                            $image_name = '';
                        }

What i did? I just insert and save the image value in another table with "Foreign Key" and change the name

$image_id = array();

instead of $image_name = array();