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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据