doukaizha5417 2014-01-25 14:38 采纳率: 0%
浏览 45
已采纳

使用PHP以逗号分隔格式在MySQL DB中添加多个图像

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条回答 默认 最新

  • duanjun7801 2014-01-25 14:59
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真