need help with upload image and getting image name to databa

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

need help with upload image and getting image name to databa

Post by mikewooten »

i'm using this upload code below.
i am a student working on an ecommerce project
i have an ecommerc website and an admin on the page that i have, i'm working on the admin part at the moment.
i want the admin to enter info into all of the fields that are provided such as the item name, item description, and item price, and item image.
here is the site that i'm working on for an example

http://www.wootenmedia.com/admintest/add3_1_3.php

i have the upload part working where i can browse for an image and upload the image into the specified folder path. the only thing that it does not do is insert the image name into the database. i would like the name to be inserted into the datbase in order for the image to show up on the ecommerc website on the products pages here is my ecommerce site i have


http://www.wootenmedia.com/wootenmusic7/guitars.php


can anyone help me out in getting the image name, which is being uploaded, to the database so that the image will show up on the products page of the ecommmerce website any help would be appreciated.
thanks
this is the code that i'm using for uploading the image and inserting the image into the database:

Code: Select all

<?php



<?php echo "<?xml version="1.0" encoding="iso-8859-1"?".">"; ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Untitled Document</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<META HTTP-EQUIV='Pragma' CONTENT='no-cache' /> 
<META HTTP-EQUIV='Cache-Control' CONTENT='no-cache' /> 
</head> 
<body> 
<form action="<? $_SERVER['PHP_SELF']; ?>" method="post" ENCTYPE="multipart/form-data"> 

Choose a product category  <br> 
<select name="txtCatID" id="select"> 
<option value="0" SELECTED>0</option> 
<option value="1">1</option> 
<option value="2">2</option> 
<option value="3">3</option> 
</select><br> 
Choose product items to add to your product pages  <br> 
   <input type="hidden" name="<?echo '$txtCatID';?>"> 
<select name="txtprodItems" id="select"> 
<option alue="Guitars" SELECTED>Guitars</option> 
<option value="Drums">Drums</option> 
<option value="Amps">Amps</option> 
<option value="Books">Books</option> 
</select><br> 
item name<br> 
<input type="text" name="txtItemName"><br> 
item description<br> 
<input type="text" name="txtItemDesc"><br> 
item price<br> 
<input type="text" name="txtItemPrice"><br> 
<? 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
?> 
<? 
##START DB Stuff Here############# 
     
// specify the directory where the uploaded file should end up 
$path = "/home/username/public_html/wootenmusic7/p_imgs/"; 
// specify the filetypes allowed 
$allowed = array('image/gif','image/pjpeg','image/jpeg','image/png');  

// specify the max filesize in bytes 
$max_size = 200000;  

if(isset($HTTP_POST_FILES['txtItemImage']))  
{  
   if(is_uploaded_file($HTTP_POST_FILES['txtItemImage
']['tmp_name']))  
  {  
   if($HTTP_POST_FILES['txtItemImage']['size'] < $max_size)  
   {  
     if(in_array($HTTP_POST_FILES['txtItemImage']['type
'],$allowed))  
    {  
     if(!file_exists($path . $HTTP_POST_FILES['txtItemImage']['name']))  
     {  
       if(@rename($HTTP_POST_FILES['txtItemImage']['tmp_n
ame'],$path.$HTTP_POST_FILES['txtItemImage']['name']))  
      {  
       $html_output = 'Upload sucessful!<br>';  
       $html_output .= 'File Name: '.$HTTP_POST_FILES['txtItemImage']['name'].'<br>';  
       $html_output .= 'File Size: '.$HTTP_POST_FILES['txtItemImage']['size'].' bytes<br>';  
       $html_output .= 'File Type: '.$HTTP_POST_FILES['txtItemImage']['type'].'<br>';  
       $image = $HTTP_POST_FILES['txtItemImage']['name'] ;  
      }else{  
       $html_output = 'Upload failed!<br>';  
       if(!is_writeable($path))  
       {  
        $html_output = 'The Directory "'.$path.'" must be writeable!<br>';  
       }else{  
        $html_output = 'an unknown error ocurred.<br>';        
       }  
      }  
     }else{  
      $html_output = 'The file already exists<br>';  
     }  
    }else{  
     $html_output = 'Wrong file type<br>';  
    }  
   }else{  
    $html_output = 'The file is too big<br>';  
   }  
  }  
}else{  
  $html_output = '<form method="post" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'">';  
  $html_output .= '<input type="file" name="txtItemImage">';  
  $html_output .= '<input type="submit" name="submit" value="upload"><input type="Reset" />';  
  $html_output .= '</form>';  
}  
echo '<html><head><title>Uploader</title></head><body>';  
echo $html_output;  
echo '</body></html>';  

   $html_output = $_POST['submit']; 
      if(isset($html_output)){ 
    include("db2.php"); 
     
    // Get a connection to the database 
    $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);             
            $txtCatID = $_POST['txtCatID']; 
            $txtprodItems = $_POST['txtprodItems']; 
            $txtItemName = $_POST['txtItemName']; 
            $txtItemDesc = $_POST['txtItemDesc']; 
            $txtItemPrice = $_POST['txtItemPrice'];            
            $txtItemImage = $_POST['txtItemImage']; 

$theSQL = "insert into items (prodItems, catid, itemName, itemDesc, itemPrice, ItemImage)"; 

$theSQL = $theSQL . " values ('$txtprodItems', '$txtCatID', '$txtItemName', '$txtItemDesc', '$txtItemPrice', '{$HTTP_POST_FILES['ItemImage']['name']}')"; 

    $result = mysql_query($theSQL);  
    echo "$theSQL"; 
    }else{ 
    echo "did not insert any image into database"; 
    } 




?>
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

try changing this:

$txtItemImage = $_POST['txtItemImage'];

to:

$txtItemImage = $HTTP_POST_FILES['txtItemImage']['name'];
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

i changed
$txtItemImage = $_POST['txtItemImage'];
to:
$txtItemImage = $HTTP_POST_FILES['txtItemImage']['name'];
like you said and still it doesn't work, what else should i try so that i can get an image to display?
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

form action is never set.. that part of the php code doesn't echo/print anything.
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

question:?
what do you mean by the form actin is never set.
would i have to echo out the contents of the form?
how would i do that?
would i just use this?

Code: Select all

<?php

echo $txtprodItems;
echo $txtCatID;
echo $txtItemName;
echo $txtItemDesc;
echo $txtItemPrice;
echo $txtItemImage;

?>
if not, what would i use to echo or what would i have to echo out?
thanks
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Two things:

1) You're missing the <input type="hidden" name="MAX_FILE_SIZE" value="30000"> line which needs to go on your file upload form (change the byte size obviously).

2) Unless you are using a version of PHP earlier than 4.1.0 the $HTTP_POST_FILES array will never exist - it's called $_FILES now.

Oh and why are you echoing out Header lines AFTER your HTML output has started? They are totally ignored. They should come before any output at all.

Cheers,

Rich
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

i have added in the <input type="hidden" name="MAX_FILE_SIZE" value="30000"> tag and also put the headers at the top of the page. what else am i missing that i should add to the page in order for these images to display on the products page from the information being entered from the form on the admin add products page?
the image still doesn't display, can anyone help me out with this code so that the image will display.
Should i change all of the $HTTP_POST_FILES to $_FILES?
thanks
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

What version of PHP do you have? If it is anything more recent than 4.0.1 then you *must* change to $_FILES.
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

Realize that not all browsers support this tag

Code: Select all

<input type="hidden" name="MAX_FILE_SIZE" value="30000">
Most do, but some don't...but putting there is good practice
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

I can't think of any browser that supports it - but PHP seems to complain if you leave it out.
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

i have changed all of the $HTTP_POST_FILES to $_FILES and still the images will not display or will not insert into the database after i have inserted information into the form. the image is uploading into the folder on my server fine, but it will not insert the name into the database and display on the products page.
can anyone help me out with this to get this to work.
what else should i change within my file to get these images to display?
thanks

here is my updated code that i have:

Code: Select all

<?
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
?>
<?php echo "<?xml version="1.0" encoding="iso-8859-1"?".">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<META HTTP-EQUIV='Pragma' CONTENT='no-cache' />
<META HTTP-EQUIV='Cache-Control' CONTENT='no-cache' />
</head>
<body>

<form action="<? $_SERVER['PHP_SELF']; ?>" method="post" ENCTYPE="multipart/form-data"> 

Choose a product category  <br>
<select name="txtCatID" id="select">
<option alue="0" SELECTED>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><br>   

Choose product items to add to your product pages  <br>
   <input type="hidden" name="<?echo '$txtCatID';?>">
<select name="txtprodItems" id="select">
<option alue="Guitars" SELECTED>Guitars</option>
<option value="Drums">Drums</option>
<option value="Amps">Amps</option>
<option value="Books">Books</option>
</select><br>

item name<br>
<input type="text" name="txtItemName"><br>

item description<br>
<input type="text" name="txtItemDesc"><br>

item price<br>
<input type="text" name="txtItemPrice"><br>

<?
	// specify the directory where the uploaded file should end up 
$path = "/home/username/public_html/wootenmusic7/p_imgs/";

// specify the filetypes allowed 
$allowed = array('image/gif','image/pjpeg','image/jpeg','image/png'); 

// specify the max filesize in bytes 
$max_size = 200000; 

if(isset($_FILES['txtItemImage'])) 
{ 
  if(is_uploaded_file($_FILES['txtItemImage']['tmp_name'])) 
  { 
   if($_FILES['txtItemImage']['size'] < $max_size) 
   { 
    if(in_array($_FILES['txtItemImage']['type'],$allowed)) 
    { 
     if(!file_exists($path . $_FILES['txtItemImage']['name'])) 
     { 
      if(@rename($_FILES['txtItemImage']['tmp_name'],$path.$_FILES['txtItemImage']['name'])) 
      { 
       $html_output = 'Upload sucessful!<br>'; 
       $html_output .= 'File Name: '.$_FILES['txtItemImage']['name'].'<br>'; 
       $html_output .= 'File Size: '.$_FILES['txtItemImage']['size'].' bytes<br>'; 
       $html_output .= 'File Type: '.$_FILES['txtItemImage']['type'].'<br>'; 
       $image = $_FILES['txtItemImage']['name'] ; 
      }else{ 
       $html_output = 'Upload failed!<br>'; 
       if(!is_writeable($path)) 
       { 
        $html_output = 'The Directory "'.$path.'" must be writeable!<br>'; 
       }else{ 
        $html_output = 'an unknown error ocurred.<br>';       
       } 
      } 
     }else{ 
      $html_output = 'The file already exists<br>'; 
     } 
    }else{ 
     $html_output = 'Wrong file type<br>'; 
    } 
   }else{ 
    $html_output = 'The file is too big<br>'; 
   } 
  } 
}else{ 

  $html_output = '<form method="post" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'">'; 
  $html_output .= '<input type="file" name="txtItemImage">'; 
  $html_output .= '<input type="submit" name="submit" value="upload"><input type="Reset" />'; 
  $html_output .= '</form>'; 
} 
echo '<html><head><title>Uploader</title></head><body>'; 
echo $html_output; 
echo '</body></html>'; 
 
   $html_output = $_POST['submit'];
      if(isset($html_output)){
	include("db2.php");
	
	// Get a connection to the database
	$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
            
            $txtCatID = $_POST['txtCatID'];
            $txtprodItems = $_POST['txtprodItems'];
            $txtItemName = $_POST['txtItemName'];
            $txtItemDesc = $_POST['txtItemDesc'];
            $txtItemPrice = $_POST['txtItemPrice'];           
            $txtItemImage = $_FILES['txtItemImage']['name'];


$theSQL = "insert into items (prodItems, catid, itemName, itemDesc, itemPrice, ItemImage)";
$theSQL = $theSQL . " values ('$txtprodItems', '$txtCatID', '$txtItemName', '$txtItemDesc', '$txtItemPrice', '{$_FILES['ItemImage']['name']}')";

    $result = mysql_query($theSQL); 
	echo "$theSQL";
	}else{
	echo "did not insert any image into database";
	}
  
 



?>
?>
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

echo out your SQL query (literally: echo $theSQL, right before or after you run the query) and paste here whatever it says.

Your query doesn't need to use the $_FILES['ItemImage'] array because you've already extracted that value in $txtItemImage - so why not just insert that instead? But anyway, post the SQL dump and let's see what it shows us.
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

i have echoed out the sql using echo $theSQL; and this is the output

The Directory "/home/username/public_html/wootenmusic7/p_imgs/" must be writeable!
insert into items (prodItems, catid, itemName, itemDesc, itemPrice, ItemImage) values ('Guitars', '0', '25', '25', '25', 'car25.jpg')

i also changed

Code: Select all

<?php

$theSQL = $theSQL . " values ('$txtprodItems', '$txtCatID', '$txtItemName', '$txtItemDesc', '$txtItemPrice', '{$_FILES['ItemImage']['name']}')";
?>
to this

Code: Select all

<?php

$theSQL = $theSQL . " values ('$txtprodItems', '$txtCatID', '$txtItemName', '$txtItemDesc', '$txtItemPrice', '$txtItemImage')";

?>
when i view the products page, i get the name of the image that is being uploaded, but still the image does not show.
thanks
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

There you go - the error is right there:

The Directory "/home/username/public_html/wootenmusic7/p_imgs/" must be writeable!

The directory obviously isn't.. you need to set the permissions on it so that the web server can copy your image files to it (depending on your server and security the settings will vary - but start with chmod 777 on p_imgs and then lock it down from there).
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

i have chmoded the directory from the beginning, i have set the permissions to 777 and it's still coming up with that error message.
do you know why its coming up with that error even though i have set the permissions to be writeable and have set it to 777?
how would i fix that error so the error doesn't show up as "directory must be writeable"?
thanks
Post Reply