-1

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) ;
        }
Bosstone
  • 189
  • 1
  • 12

1 Answers1

0

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
ivanivan
  • 2,155
  • 2
  • 10
  • 11