Jump to content

Upload Image and update MySQL Database Problem


dutchboy

Recommended Posts

Hi all thanks if you are taking time to read this.

 

I have a problem with this script i am using and was wondering if anyone could help me out.

 

I am creating a simple CMS and have managed to get the data entry for the site working very well with the original version of this script:

 

<? 
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}

$id = $_GET['id'];

$idir = "dbimages/";

$tdir = "dbimages/thimages/";

$twidth = "120";

$theight = "120";

$photo1=( $_FILES['photo1']['name']);

$photo2=( $_FILES['photo2']['name']);

$photo3=( $_FILES['photo3']['name']);

$photo4=( $_FILES['photo4']['name']);

$photo5=( $_FILES['photo5']['name']);



mysql_connect("localhost", "user", "pass") or die(mysql_error()) ; 

mysql_select_db("db") or die(mysql_error()) ;

mysql_query("UPDATE table SET photo1='$photo1', photo2='$photo2', photo3='$photo3', photo4='$photo4', photo5='$photo5' WHERE id='$id' ") ;



  $url1 = $_FILES['photo1']['name']; 

  if ($_FILES['photo1']['type'] == "image/jpg" || $_FILES['photo1']['type'] == "image/jpeg" || $_FILES['photo1']['type'] == "image/pjpeg") { 

    $file_ext = strrchr($_FILES['photo1']['name'], '.');   

    $copy = copy($_FILES['photo1']['tmp_name'], "$idir" . $_FILES['photo1']['name']);

    if ($copy) { 

      $simg = imagecreatefromjpeg("$idir" . $url1);

      $currwidth = imagesx($simg);

      $currheight = imagesy($simg); 

      if ($currheight > $currwidth) {

         $zoom = $twidth / $currheight; 

         $newheight = $theight;

         $newwidth = $currwidth * $zoom;

      } else {

        $zoom = $twidth / $currwidth; 

        $newwidth = $twidth;

        $newheight = $currheight * $zoom;

      } 

      $dimg = imagecreate($newwidth, $newheight);

      imagetruecolortopalette($simg, false, 256); 

      $palsize = ImageColorsTotal($simg); 

      for ($i = 0; $i < $palsize; $i++) {

       $colors = ImageColorsForIndex($simg, $i);

       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);

      } 

      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); 

      imagejpeg($dimg, "$tdir" . $url1);

      imagedestroy($simg);

      imagedestroy($dimg); 
  
  }
  
  $url2 = $_FILES['photo2']['name'];
  
 if ($_FILES['photo2']['type'] == "image/jpg" || $_FILES['photo2']['type'] == "image/jpeg" || $_FILES['photo2']['type'] == "image/pjpeg") { 

    $file_ext = strrchr($_FILES['photo2']['name'], '.');   

    $copy = copy($_FILES['photo2']['tmp_name'], "$idir" . $_FILES['photo2']['name']);

    if ($copy) { 

      $simg = imagecreatefromjpeg("$idir" . $url2);

      $currwidth = imagesx($simg);

      $currheight = imagesy($simg); 

      if ($currheight > $currwidth) {

         $zoom = $twidth / $currheight; 

         $newheight = $theight;

         $newwidth = $currwidth * $zoom;

      } else {

        $zoom = $twidth / $currwidth; 

        $newwidth = $twidth;

        $newheight = $currheight * $zoom;

      } 

      $dimg = imagecreate($newwidth, $newheight);

      imagetruecolortopalette($simg, false, 256); 

      $palsize = ImageColorsTotal($simg); 

      for ($i = 0; $i < $palsize; $i++) {

       $colors = ImageColorsForIndex($simg, $i);

       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);

      } 

      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); 

      imagejpeg($dimg, "$tdir" . $url2);

      imagedestroy($simg);

      imagedestroy($dimg);
  
  	  }
  
  $url3 = $_FILES['photo3']['name'];
  
 if ($_FILES['photo3']['type'] == "image/jpg" || $_FILES['photo3']['type'] == "image/jpeg" || $_FILES['photo3']['type'] == "image/pjpeg") { 

    $file_ext = strrchr($_FILES['photo3']['name'], '.');   

    $copy = copy($_FILES['photo3']['tmp_name'], "$idir" . $_FILES['photo3']['name']);

    if ($copy) { 

      $simg = imagecreatefromjpeg("$idir" . $url3);

      $currwidth = imagesx($simg);

      $currheight = imagesy($simg); 

      if ($currheight > $currwidth) {

         $zoom = $twidth / $currheight; 

         $newheight = $theight;

         $newwidth = $currwidth * $zoom;

      } else {

        $zoom = $twidth / $currwidth; 

        $newwidth = $twidth;

        $newheight = $currheight * $zoom;

      } 

      $dimg = imagecreate($newwidth, $newheight);

      imagetruecolortopalette($simg, false, 256); 

      $palsize = ImageColorsTotal($simg); 

      for ($i = 0; $i < $palsize; $i++) {

       $colors = ImageColorsForIndex($simg, $i);

       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);

      } 

      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); 

      imagejpeg($dimg, "$tdir" . $url3);

      imagedestroy($simg);

      imagedestroy($dimg);
  
  	  }
  
  $url4 = $_FILES['photo4']['name'];
  
 if ($_FILES['photo4']['type'] == "image/jpg" || $_FILES['photo4']['type'] == "image/jpeg" || $_FILES['photo4']['type'] == "image/pjpeg") { 

    $file_ext = strrchr($_FILES['photo4']['name'], '.');   

    $copy = copy($_FILES['photo4']['tmp_name'], "$idir" . $_FILES['photo4']['name']);

    if ($copy) { 

      $simg = imagecreatefromjpeg("$idir" . $url4);

      $currwidth = imagesx($simg);

      $currheight = imagesy($simg); 

      if ($currheight > $currwidth) {

         $zoom = $twidth / $currheight; 

         $newheight = $theight;

         $newwidth = $currwidth * $zoom;

      } else {

        $zoom = $twidth / $currwidth; 

        $newwidth = $twidth;

        $newheight = $currheight * $zoom;

      } 

      $dimg = imagecreate($newwidth, $newheight);

      imagetruecolortopalette($simg, false, 256); 

      $palsize = ImageColorsTotal($simg); 

      for ($i = 0; $i < $palsize; $i++) {

       $colors = ImageColorsForIndex($simg, $i);

       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);

      } 

      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); 

      imagejpeg($dimg, "$tdir" . $url4);

      imagedestroy($simg);

      imagedestroy($dimg);
  	  }
  
  $url5 = $_FILES['photo5']['name'];
  
 if ($_FILES['photo5']['type'] == "image/jpg" || $_FILES['photo5']['type'] == "image/jpeg" || $_FILES['photo5']['type'] == "image/pjpeg") { 

    $file_ext = strrchr($_FILES['photo5']['name'], '.');   

    $copy = copy($_FILES['photo5']['tmp_name'], "$idir" . $_FILES['photo5']['name']);

    if ($copy) { 

      $simg = imagecreatefromjpeg("$idir" . $url5);

      $currwidth = imagesx($simg);

      $currheight = imagesy($simg); 

      if ($currheight > $currwidth) {

         $zoom = $twidth / $currheight; 

         $newheight = $theight;

         $newwidth = $currwidth * $zoom;

      } else {

        $zoom = $twidth / $currwidth; 

        $newwidth = $twidth;

        $newheight = $currheight * $zoom;

      } 

      $dimg = imagecreate($newwidth, $newheight);

      imagetruecolortopalette($simg, false, 256); 

      $palsize = ImageColorsTotal($simg); 

      for ($i = 0; $i < $palsize; $i++) {

       $colors = ImageColorsForIndex($simg, $i);

       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);

      } 

      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); 

      imagejpeg($dimg, "$tdir" . $url5);

      imagedestroy($simg);

      imagedestroy($dimg);
  
  header( 'Location:genxml.php' ) ;

    } else { 

      print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; 
}
}
}
}
}
}
?>

 

This page is fed from a simple html form and uploads the pictures and creates the thumbnails on the fly and stores them on the server while adding the file name to my database along with the other info submitted.

 

This works with no problems. I then needed to create another script that allowed me to delete the images by $id from the server which works, and now i would like to adjust the above script so that i can update the images and filenames only, into my database on the relevant field (by $id).

 

For ex. I upload all the details one day( including images ), and the next day i want to change all the images so i run my delete image script against the id ($id) and it deletes the images from the folders on the servers.

 

I then want to choose 5 different images and upload them and add the filenames to an existing database entry. So I delete that particular entries images and carry on.

 

However, as i changed my query to 'update' (in the code above) from 'insert into' (from the original upload page) the filenames do not update in the database, although the images are still uploaded and thumbnails created ect... 

 

I am having no problems updating other info through my CMS just this problem with adding new/different image filenames to an existing entry.

 

Sorry this is long, hope i have been clear enough.

 

Thanks.

Link to comment
Share on other sites

wouldn't all the photos have different IDs?

 

thus it would only update the one photo with the id?

 

in which case if that is happening.. just put the script into a forloop

$count=5;
for($x=0;$x<=$count;$x++)
{
mysql_query("UPDATE table SET photo1='$photo1', photo2='$photo2', photo3='$photo3', photo4='$photo4', photo5='$photo5' WHERE id='$id".$x."' ") ;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.