[solved] uploading image name to db

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

Post Reply
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

[solved] uploading image name to db

Post by tom.cull »

I have a page that finds a db record and passes the id onto an imgage upload page.

The uploading part works fine - i can save images to the server. No problem. What I also need is when I upload the image, the name of the image (eg example.gif) gets copied into a image_name field on my db.

This is what I have to upload the image:


Code: Select all

<form name="form1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="imagefile">
<br>
<input type="submit" name="Submit" value="Submit"> 

<?

if(isset( $Submit ))
{
//If the Submitbutton was pressed do:

if ($_FILES['imagefile']['type'] == "image/gif"){ 

copy ($_FILES['imagefile']['tmp_name'], "images/classpix/".$_FILES['imagefile']['name'])
    or die ("Could not copy"); 
	
	echo "";
        echo "Name: ".$_FILES['imagefile']['name']."";
        echo "Size: ".$_FILES['imagefile']['size']."";
        echo "Type: ".$_FILES['imagefile']['type']."";
        echo "Copy Done....";
        } 
		
		else {
            echo "<br><br>";
            echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>";
        }
} 
?> </form>

I just cant figure out how to add the stuff to update the db.

Any help welcome!

Burrito: Please use

Code: Select all

tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting PHP Code In The Forums[/url][/size]
Last edited by tom.cull on Wed Sep 14, 2005 5:17 am, edited 1 time in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post by tom.cull »

hi - ok - ive got this - the page goes thru and image uploads but the db isnt updated:

Code: Select all

<form name="form1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="imagefile">
<br>
<input type="submit" name="Submit" value="Submit"> 

<?

if(isset( $Submit ))
{
//If the Submitbutton was pressed do:

if ($_FILES['imagefile']['type'] == "image/gif"){ 

copy ($_FILES['imagefile']['tmp_name'], "images/classpix/".$_FILES['imagefile']['name'])
    or die ("Could not copy"); 
	
	echo "";
        echo "Name: ".$_FILES['imagefile']['name']."";
        echo "Size: ".$_FILES['imagefile']['size']."";
        echo "Type: ".$_FILES['imagefile']['type']."";
        echo "Copy Done....";
        } 
		
		else {
            echo "<br><br>";
            echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>";
        }
} 

$username="xxxx_xxxx";
$password="xxxx";
$database="xxxx_xxxx";

   // saving script
   
   // connect to the server
   mysql_connect( 'localhost', $username, $password )
      or die( "Error! Could not connect to database: " . mysql_error() );
   
   // select the database
   mysql_select_db( $database )
      or die( "Error! Could not select the database: " . mysql_error() );


// get the variables from the URL GET string
   $id = $_GET['id'];

//set image_name
	$image_name = $_FILES['imagefile']['name'].

 // if $id is not defined, we have a new entry, otherwise update the old entry
  
      $query = "UPDATE newsstuf_usedcars SET image_name='$image_name' WHERE `id`='$id'";

// save the info to the database
  
   
   $results = mysql_query( $query );


?> </form>
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

tom.cull wrote:hi - ok - ive got this - the page goes thru and image uploads but the db isnt updated:

Code: Select all

<form name="form1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="imagefile">
<br>
<input type="submit" name="Submit" value="Submit"> 

<?

if(isset( $Submit ))
{
//If the Submitbutton was pressed do:

if ($_FILES['imagefile']['type'] == "image/gif"){ 

copy ($_FILES['imagefile']['tmp_name'], "images/classpix/".$_FILES['imagefile']['name'])
    or die ("Could not copy"); 
	
	echo "";
        echo "Name: ".$_FILES['imagefile']['name']."";
        echo "Size: ".$_FILES['imagefile']['size']."";
        echo "Type: ".$_FILES['imagefile']['type']."";
        echo "Copy Done....";
        } 
		
		else {
            echo "<br><br>";
            echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>";
        }
} 

$username="xxxx_xxxx";
$password="xxxx";
$database="xxxx_xxxx";

   // saving script
   
   // connect to the server
   mysql_connect( 'localhost', $username, $password )
      or die( "Error! Could not connect to database: " . mysql_error() );
   
   // select the database
   mysql_select_db( $database )
      or die( "Error! Could not select the database: " . mysql_error() );


// get the variables from the URL GET string
   $id = $_GET['id'];

//set image_name
	$image_name = $_FILES['imagefile']['name'].

 // if $id is not defined, we have a new entry, otherwise update the old entry
  
      $query = "UPDATE newsstuf_usedcars SET image_name='$image_name' WHERE `id`='$id'";

// save the info to the database
  
   
   $results = mysql_query( $query );


?> </form>


just print the query and you will get to know wheather the is correct or not? then you can findout wots the probelm is ?
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post by tom.cull »

ok - I get this when I echo the query:

UPDATE newsstuf_usedcars SET image_name='' WHERE `id`='8'

i guess that it means that the image name is not being passed to my $image_name.

the code that I have for this is: (taken from above code)

Code: Select all

//set image_name
	$image_name = $_FILES['imagefile']['name'].
i guess that is not the right way to do it? This is the first time I have worked with images/php/mysql! (can you guess!)

thanks
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Code: Select all

if(is_uploaded_file($_FILES['file']['tmp_name']))
		{
			move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
			$file = $_FILES['file']['name'];
                 }
try this bunch of code.
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post by tom.cull »

i treid putting that in, then it didnt even upload the image.

I think the problem is just with passing the image name to $image_name - does any one know the correct way to do it using what i have already got?
Post Reply