doom910730 2018-07-05 20:49
浏览 69

PHP Foreach循环:只向表格插入一次[复制]

This question already has an answer here:

I have a image upload Script where I want to insert data into 2 tables.

Table: Dresses where description, brand, year will be inserted Table: Images where I insert dress_id and link of the image

I have a foreach loop where - in the first loop - the dress and the first image should be inserted, in the second,... loop only the image to table images.

Is this possible? I am using mysql_insert_id() is this even possible?

Thank you for any assistance and advice!

Kind Regards, Stefan

here is my code so far.

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 > 400097152){
        $errors[]='Maximale Filegröße: 100MB';
}


    $file_name = 'image_' . date('Y-m-d-H-i-s') . '_' . uniqid() . '.jpg';
    $url = 'dresses/' . $_POST['jahr']. '/';
    $link = $domain . '/dresses/' . $_POST['jahr'] . '/'. $file_name;


    $name_insert = mysql_real_escape_string($_POST['name']);
    $brand_insert = mysql_real_escape_string($_POST['brand']);
    $style_insert = mysql_real_escape_string($_POST['style']);
    $jahr_insert = mysql_real_escape_string($_POST['jahr']);
    $desc_insert = mysql_real_escape_string($_POST['desc']);


    mysql_query("SET CHARACTER SET 'utf8'");
    $sql_upload="INSERT into dresses (model,brand,style,jahr,beschreibung) VALUES('$name_insert','$brand_insert','$style_insert','$jahr_insert','$desc_insert')";   
    $result = mysql_query($sql_upload,$db);

    $dress_id = mysql_insert_id();

    $sql_image = "Insert into images (dress_id, url) VALUES ('$dress_id','$link')";
    $result = mysql_query($sql_image, $db);


    $desired_dir= $url;
    if(empty($errors)==true){
        if(is_dir($desired_dir)==false){
            mkdir("$desired_dir", 0755, true);      // Create directory if it does not exist
        }
        if(is_dir("$desired_dir/".$file_name)==false){
            move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            $source_img = "$desired_dir/".$file_name;
            $destination_img = $desired_dir."/".$file_name;
            $d = compress($source_img, $destination_img, 50);
        }else{                                  // rename the file if another one exist
            $new_dir="$desired_dir/".$file_name.time();
             rename($file_tmp,$new_dir) ;
        }
</div>
  • 写回答

1条回答 默认 最新

  • dousi3362 2018-07-05 22:01
    关注

    First, as Aleks G said, stop using mysql_ and start using PDO or mysqli and prepared statements.

    Second, rework your processing. Collect your dress information, insert the dress info, get the reference to it, and then loop through your array of files. No reason for the dress insert handling to be in the loop where the files are processed

    Basically, moving this block of code to before your foreach starts -

    $name_insert = mysql_real_escape_string($_POST['name']);
    $brand_insert = mysql_real_escape_string($_POST['brand']);
    $style_insert = mysql_real_escape_string($_POST['style']);
    $jahr_insert = mysql_real_escape_string($_POST['jahr']);
    $desc_insert = mysql_real_escape_string($_POST['desc']);
    
    
    mysql_query("SET CHARACTER SET 'utf8'");
    $sql_upload="INSERT into dresses (model,brand,style,jahr,beschreibung) VALUES('$name_insert','$brand_insert','$style_insert','$jahr_insert','$desc_insert')";   
    $result = mysql_query($sql_upload,$db);
    
    $dress_id = mysql_insert_id();
    
    // start foreach after this
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器