Page 1 of 1

[solved] uploading image name to db

Posted: Tue Sep 13, 2005 9:59 am
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]

Posted: Tue Sep 13, 2005 11:37 am
by josh

Posted: Wed Sep 14, 2005 3:13 am
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>

Posted: Wed Sep 14, 2005 3:35 am
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 ?

Posted: Wed Sep 14, 2005 3:54 am
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

Posted: Wed Sep 14, 2005 4:06 am
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.

Posted: Wed Sep 14, 2005 4:43 am
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?