Author Topic: cleaning data in db  (Read 817 times)

0 Members and 1 Guest are viewing this topic.

Offline runnerjpTopic starter

  • Addict
  • Posts: 2,165
    • View Profile
cleaning data in db
« on: February 12, 2008, 08:13:43 AM »
with this code
Code: [Select]
<?php

session_start
();
//load the config file
include("config.php");
require_once 
'../settings.php';


//if the for has submittedd
if (isset($_POST['upForm'])){

       
$file_type $_FILES['imgfile']['type'];
       
$file_name $_FILES['imgfile']['name'];
       
$file_size $_FILES['imgfile']['size'];
       
$file_tmp $_FILES['imgfile']['tmp_name'];

       
//check if you have selected a file.
       
if(!is_uploaded_file($file_tmp)){
          echo 
"Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
          exit(); 
//exit the script and don't do anything else.
       
}
       
//check file extension
       
$ext strrchr($file_name,'.');
       
$ext strtolower($ext);
       if ((
$extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo 
"Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
          exit();
       }
       
//get the file extension.
       
$getExt explode ('.'$file_name);
       
$file_ext $getExt[count($getExt)-1];

//get users ID
    
$id $_SESSION['user_id'];
    
      
      
//get the new width variable.
       
$ThumbWidth $img_thumb_width;

       
//keep image type
       
if($file_size){
          if(
$file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               
$new_img imagecreatefromjpeg($file_tmp);
           }elseif(
$file_type == "image/x-png" || $file_type == "image/png"){
               
$new_img imagecreatefrompng($file_tmp);
           }elseif(
$file_type == "image/gif"){
               
$new_img imagecreatefromgif($file_tmp);
           }
           
//list width and height and keep height ratio.
           
list($width$height) = getimagesize($file_tmp);
           
$imgratio=$width/$height;
           if (
$imgratio>1){
              
$newwidth $ThumbWidth;
              
$newheight $ThumbWidth/$imgratio;
           }else{
                 
$newheight $ThumbWidth;
                 
$newwidth $ThumbWidth*$imgratio;
           }
           
//function for resize image.
           
if (function_exists(imagecreatetruecolor)){
           
$resized_img imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die(
"Error: Please make sure you have GD library ver 2+");
           }
           
imagecopyresized($resized_img$new_img0000$newwidth$newheight$width$height);
           
//save image
           
ImageJpeg ($resized_img,"$path_thumbs/$id.$file_ext");
           
ImageDestroy ($resized_img);
           
ImageDestroy ($new_img);
           
//print message
           
echo "<br>Image Thumb: <a href=\"$path_thumbs/$id.$file_ext\">$path_thumbs/$id.$file_ext</a>";
        }

        
//upload the big image
        
move_uploaded_file ($file_tmp"$path_big/$id.$file_ext");

        echo 
"<br>Image Big: <a href=\"$path_big/$id.$file_ext\">$path_big/$id.$file_ext</a>";

        echo 
"<br><br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
        
        
mysql_query("INSERT INTO user_images (user_id, ext) 
VALUES ('"
.$id."', '".$file_ext."')") or die (mysql_error());

}else{ 
//if the form hasn't been submitted.

      //print the form
      
echo "<script>
      function view_img(img_name){
         document[img_name].src = upForm.imgfile.value;
            document[img_name].width = 150;
      }
      </script>\n\n
      <br><h3>:: Browse an Image to Upload:</h3>\n
      <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"
$_SERVER[PHP_SELF]\">\n
      <input type=\"file\" name=\"imgfile\" onchange=\"javascript:view_img('img_vv');\"> <img src='' name='img_vv' width='0'><br>\n
      Image width will resize to <b>
$img_thumb_width</b> with height ratio.
      <br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n
      </form>
      <a href=\"view_gallery.php\">View Images</a>"
;
      }    

?>


it enters the users id and file type... but if a user chnages an image then the last data is still in db like shown in image




what i need to do is wipe the old one to exchange it with the new 1... how would i do this?

Offline Cognito

  • Irregular
  • Posts: 26
    • View Profile
Re: cleaning data in db
« Reply #1 on: February 12, 2008, 08:49:20 AM »
you need to add some code to check to see if the user_id already exists in the database. if it does you need to use a replace statement in your sql query else you need to use insert.

Hope that helps.

Offline runnerjpTopic starter

  • Addict
  • Posts: 2,165
    • View Profile
Re: cleaning data in db
« Reply #2 on: February 12, 2008, 08:59:19 AM »
ok any where that i can look up on this?

Online Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,755
    • View Profile
Re: cleaning data in db
« Reply #3 on: February 12, 2008, 09:04:07 AM »
First off you should place your query into a variable instead of directly into the mysql_query() command. Makes it MUCH easier to debug later on.

In this case I would just use an ON DUPLICATE KEY UPDATE in the query.

You can just change your query as follows:
Code: [Select]
$query = "INSERT INTO user_images (user_id, ext) VALUES ('$id', '$file_ext')
          ON DUPLICATE KEY UPDATE ext = '$file_ext'";
mysql_query($query) or die(mysql_error());
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline runnerjpTopic starter

  • Addict
  • Posts: 2,165
    • View Profile
Re: cleaning data in db
« Reply #4 on: February 12, 2008, 09:13:12 AM »
ahhh thats good works just the way i wanted it to... ty :) i will now be able to apply this to other things

is there away where the images stored in my files can be deleted if a duplicate id is there??

Online Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,755
    • View Profile
Re: cleaning data in db
« Reply #5 on: February 12, 2008, 11:31:17 AM »
You would want to do that before updating so you know the extension of the old file to be deleted. However, instead of going to all of this trouble I would suggest converting all the images to one file type. Then you don'yt need the database for this at all. Here are a few possibilities:

http://www.phpit.net/article/image-manipulation-php-gd-part1/4/
http://www.phpclasses.org/browse/file/8221.html
http://phpclasses.ca/browse/package/2073.html
« Last Edit: February 12, 2008, 11:31:50 AM by mjdamato »
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net