Insert Database Problem!! Please HELP!!!

See all posts Reply

Insert Database Problem!! Please HELP!!! new!
by nedim, 13 years ago
Hi guys;

Here is my problem:

The code upload images perfectly. When I use "image upload" filed the code upload 2 files to my server. One of is my original file and the other is resized file. I want to write BOTH names and paths to mysql database. Original uploaded file name and resized file name. But it always write the resized name only.


And here is my codes beow:

This is the upload.php part of the code;
// we create an instance of the class, giving as argument the PHP object
// corresponding to the file field from the form
// All the uploads are accessible from the PHP object $_FILES
$handle = new Upload($_FILES['my_field']);

// then we check if the file has been uploaded properly
// in its *temporary* location in the server (often, it is /tmp)  
if ($handle->uploaded) {

  // yes, the file is on the server
  // below are some example settings which can be used if the uploaded file is an image.
  $handle->image_resize            = true;
  $handle->image_ratio_y           = true;
  $handle->image_x                 = 300;

  // now, we start the upload 'process'. That is, to copy the uploaded file
  // from its temporary location to the wanted location
  // It could be something like $handle->Process('/home/www/my_uploads/');
  $handle->Process($dir_dest);

  // we check if everything went OK
  if ($handle->processed) {
    // everything was fine !
    echo 'file uploaded with success';
    $info = getimagesize($handle->file_dst_pathname);
    echo '  ' . $info['mime'] . '  -  ' . $info[0] . ' x ' . $info[1] .'  -  ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
    echo '  link to the file just uploaded: '.$dir_pics.'/' . $handle->file_dst_name . '';
  } else {
    // one error occured
    echo 'file not uploaded to the wanted location';
    echo '  Error: ' . $handle->error . '';
  }

  // we now process the image a second time, with some other settings
  $handle->image_resize            = true;
  $handle->image_ratio_crop        = true;
  $handle->image_y                 = 150;
  $handle->image_x                 = 120;
  $handle->image_border            = 5;
  $handle->image_border_color      = '#FF0000';
  $handle->file_name_body_pre = 'thumb_';
  $handle->Process($dir_dest);

  // we check if everything went OK
  if ($handle->processed) {
    // everything was fine !
    echo 'file uploaded with success';
    $info = getimagesize($handle->file_dst_pathname);
    echo '  ' . $info['mime'] . '  -  ' . $info[0] . ' x ' . $info[1] .'  -  ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
    echo '  link to the file just uploaded: '.$dir_pics.'/' . $handle->file_dst_name . '';
  } else {
    // one error occured
    echo 'file not uploaded to the wanted location';
    echo 'Error: ' . $handle->error . '';
   }

  // we delete the temporary files
  $handle-> Clean();

} else {
  // if we're here, the upload file failed for some reasons
  // i.e. the server didn't receive the file
  echo 'file not uploaded on the server';
  echo '  Error: ' . $handle->error . '';
}



And this is the index.php which is write data in mysql:

function add() {
  
  include("upload.php");
    
  $cname = $_POST["cname"];
  $cheader = $_POST["header"];
  $detail = $_POST["detail"];
  $cthumb = $dir_pics.'/' . $handle->file_dst_name ;
  $cphoto = $dir_pics.'/' . $handle->file_dst_name ;

  $insert = mysql_query("insert into port(wbrand , wheader , wdetail , wthumb , wpic)
    values ('$cname' , '$cheader' , '$detail' , '$cthumb' , '$cphoto')");
    if ($insert) {
      include ("all.php");
    } else {
      include ("all.php");
    }
  }
}

I know there is several posts about that issue guys and I try all of them. It still doesn't work well.

Please, Please help or I'm goin' CRAZYYY :)

Thanks already
BYEReply
Re: Insert Database Problem!! Please HELP!!! new!
by colin, 13 years ago
The logic in your code is flawed. You should read file_dst_name after each call to process(). Not to mention that you store twice the same variable in your database:
$cthumb = $dir_pics.'/' . $handle->file_dst_name ;
$cphoto = $dir_pics.'/' . $handle->file_dst_name ;

But more importantly, you shouldn't use the file upload.php. it is just provided as an example of implementation. You should write your own code.Reply