0

I am new to PHP Programming. I want to browse multiple images at once. During an on-click of submit button I want to save all selected image path in comma separated format in single row. I am getting confused to where i put insert query statement.

Currently I am using following code for this functionality

PHP Code

<?php
$hostname_connect= "localhost";
$database_connect= "test";
$username_connect= "root";
$password_connect= "";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
@mysql_select_db($database_connect) or die (mysql_error());

if(isset($_FILES['files']))
{
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name )
    {
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152)
        {
            $errors[]='File size must be less than 2 MB';
        }

        $desired_dir="user_data";
        if(empty($errors)==true)
        {
            if(is_dir($desired_dir)==false)
            {
                // Create directory if it does not exist
                mkdir("$desired_dir", 0700);        
            }

            if(is_dir("$desired_dir/".$file_name)==false)
            {
                move_uploaded_file($file_tmp,"user_data/".$file_name);
            }
            else
            {   
                 //rename the file if another one exist
                 $new_dir="user_data/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
            //mysql_query($query);          
        }
        else
        {
                print_r($errors);
        }

        echo $file_name = $file_name.",";
        $query="INSERT into upload_data (FILE_NAME,FILE_SIZE,FILE_TYPE) VALUES('$file_name','$file_size','$file_type'); ";
        mysql_query($query);
    }


    if(empty($error))
    {
        echo "Success";
    }       
}
?>    

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple/>
    <input type="submit"/>
</form>

How do I save the images to the database in a comma separated format?

1 Answer 1

1

Construct comma separated string from the submitted image paths and then insert that into the database. Insert should go after foreach loop.

Sign up to request clarification or add additional context in comments.

1 Comment

but how i create comma separated string.i try this echo $file_name = $file_name.","; is it correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.