使用单个提交按钮通过php将多个图像插入数据库

时间:2013-10-31 20:56:08

标签: php mysql

在这里,我试图将多个图像插入到数据库中,但是这个代码没有发生,我只将图像的名称插入数据库并将图像移动到特定的路径。对于单个图像上传,它的工作,当我尝试上传多个图像然后它出错。请建议我你的想法。向所有人推进thanx。我是php的新手,只是想学习。

 $img_name = $_FILES['banner']['name'];
 $img_size = $_FILES['banner']['size'];
 $img_temp = $_FILES['banner']['tmp_name'];
 $pht_name = $_FILES['photos']['name'];
 $pht_size = $_FILES['photos']['size'];
 $pht_temp = $_FILES['photos']['tmp_name'];
 $pht_name_a = $_FILES['photos_a']['name'];
 $pht_size_a = $_FILES['photos_a']['size'];
 $pht_temp_a = $_FILES['photos_a']['tmp_name'];
 $pht_name_b = $_FILES['photos_b']['name'];
 $pht_size_b = $_FILES['photos_b']['size'];
 $pht_temp_b = $_FILES['photos_b']['tmp_name'];

include "_lib/_req/conn.php";
// section 1 (page contents)
$name = $pg_name . '_540x165_' . '.' . $img_ext;
$path = "_img\\gallery\\540x165\\";
$target_path = $path . $name;
if(move_uploaded_file($img_temp, $target_path)){
$insert = "INSERT INTO page_content (p_c_id, p_c_name, p_c_link, p_c_group, p_c_text, p_c_img, p_c_tag) 
VALUES ('', '$pg_name', '$pg_link', '$pg_group', '$text', '$name', '$pg_tag')";
$query = mysql_query($insert);              
}
else{ die(mysql_error()); }
$md5_6 = substr(md5(rand()),0,6);
$name_a = $pg_name . '_258x130_' . $md5_6 . '.' . $pht_ext;
$path_a = "_img\\gallery\\258x130\\";
$target_path_a = $path_a . $name_a;
if(move_uploaded_file($pht_temp, $target_path_a)){
    $insert_a = "INSERT INTO page_photos (p_p_id, p_p_name, p_p_img) 
    VALUES ('', '$pg_name', '$name_a')";
    $query_a = mysql_query($insert_a);
}
else{ die(mysql_error()); }
// page photo no.2
$md5_7 = substr(md5(rand()),0,7);
$name_b = $pg_name . '_258x130_' . $md5_7 . '.' . $pht_ext_a;
$path_b = "_img\\gallery\\258x130\\";
$target_path_b = $path_b . $name_b;
if(move_uploaded_file($pht_temp_a, $target_path_b)){
    $insert_b = "INSERT INTO page_photos (p_p_id, p_p_name, p_p_img) 
    VALUES ('', '$pg_name', '$name_b')";
    $query_b = mysql_query($insert_b);
}
else{ die(mysql_error()); }

1 个答案:

答案 0 :(得分:0)

希望它可以帮到你,

请在您的代码中使用以下查询相应部分,假设'p_p_id'是主键,因此我已从查询中删除。

     //page_content
     $insert = "INSERT INTO page_content (p_c_name, p_c_link, p_c_group, p_c_text, p_c_img, p_c_tag) VALUES ('$pg_name', '$pg_link', '$pg_group', '$text', '$name', '$pg_tag')";

     //page_photos
     $insert_a = "INSERT INTO page_photos (p_p_name, p_p_img) VALUES ('$pg_name', '$name_a')";

     //page_photos
     $insert_b = "INSERT INTO page_photos (p_p_name, p_p_img)  VALUES ('$pg_name', '$name_b')";

您能否查看上传路径及其权限以及

   $path = "_img\\gallery\\540x165\\";   

Change it as 

   $path = "_img/gallery/540x165/";   //in all 

对于多文件上传,

输入文件标签应为,

   <input type='file' name='file[]' multiple>

然后为多个文件上传示例PHP代码,如下所示,

         $count=0;             
         foreach ($_FILES['file']['name'] as $filename) 
        {
            $target='';//folder path
            $tmp=$_FILES['file']['tmp_name'][$count];
            $count=$count + 1;
            $target=$target.'/'.basename($filename);
            move_uploaded_file($tmp,$target);
              // your insert query here              

        }

如需更多帮助,http://php.net/manual/en/features.file-upload.multiple.php