Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
having problems adding data to database
Old 02-12-2008, 07:10 AM having problems adding data to database
Banned

Posts: 23
Trades: 0
hey guys i have this code to add users id and image exe to database so i can pull it up later on

PHP Code:
<?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>";

}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>"
;
      }
     
mysql_query("INSERT INTO user_images (user_id, ext) 
VALUES ('
$id', '$file_ext')") or die (mysql_error());
?>
but when it inserts the data into my database it also insers 2 blank data showin in the image below but why is this happening and how can i fix it in my code??

runnerjp is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-12-2008, 07:56 AM Re: having problems adding data to database
Skilled Talker

Posts: 71
Trades: 0
Code:
mysql_query("INSERT INTO user_images (user_id, ext) 
VALUES ('".$id."', '".$file_ext."')") or die (mysql_error());
Try that.
CrazeDizzleD is offline
Reply With Quote
View Public Profile
 
Old 02-12-2008, 08:01 AM Re: having problems adding data to database
Banned

Posts: 23
Trades: 0
nope sorry lol for some reason it added 2 fields again :S

runnerjp is offline
Reply With Quote
View Public Profile
 
Old 02-12-2008, 08:04 AM Re: having problems adding data to database
Skilled Talker

Posts: 71
Trades: 0
Comment out the query string and echo those variables, see if they contain a value.
CrazeDizzleD is offline
Reply With Quote
View Public Profile
 
Old 02-12-2008, 08:06 AM Re: having problems adding data to database
Banned

Posts: 23
Trades: 0
ahhh wait got it... i have added it in the wrong place... should have looked like this

PHP Code:
<?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>"
;
      }
     

?>
runnerjp is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to having problems adding data to database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28149 seconds with 12 queries