在多个文件上传(PHP / Mysql)中,图像存储在单列中。想要将每个图像存储在具有相同ID的单行中

时间:2016-06-24 05:59:56

标签: php mysql

table1

现在图像存储在单列中。我想将每个图像存储在一行中。

  

例如。 id“1149”有两个图像,现在我希望图像1136524631.png在下一行中具有相同的id,即1149.类似于id“1150”。

这是代码 - >

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')" );

2 个答案:

答案 0 :(得分:0)

为什么你想要它们在同一行?

你不能在一行内有一行,所以你必须连接图像,如果你想要它们在同一行(后来你会为此而生气)。执行此操作的好方法是为具有此表的外键的图像创建另一个表,并在选择数据时将其连接

答案 1 :(得分:0)

<强>解答:  我解决了查询。请参阅以下代码段

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 = '';
                        }

我做了什么?我只是在&#34;外键&#34;中插入并将图像值保存在另一个表中。并更改名称

$image_id = array();

而不是$image_name = array();