Jump to content

Uploading Image to Directory, Filename to Database


JsusSalv

Recommended Posts

Hello:

 

    I am attempting to upload an image to my server and store it in a directory.  The name of the image is to go to a MySQL database.  I've tested the script and it works.  Now, my issue comes when I try to save the filename to the database for a particular user.  The script works fine when the SQL is set to INSERT.  While this does write the filename to the database, it also creates a new user within the member system.  NOT what I need.  I need the SQL command to save the filename to the database for the particular user that uploaded the photo.  Any suggestions or ideas?  Below is the code I have for the files I am using:

 

 

This code excerpt is for initiating the file upload sequence:

echo "<a href=\"#\" onclick=\"javascript:window.open('/directory/upload_thumbnail.php?id=$username','popup','top=125,left=125,width=600,height=525');\" id=\"linkStyle\">Upload Thumbnail Photo</a>";

 

This is the file that loads within the pop-up window mentioned above:

<?php

session_start();

 

$username = $_SESSION["username"];

 

echo '<form enctype="multipart/form-data" action="thumbnailpix.php?id='.$username.'" method="POST">';

echo 'Thumbnail Photo: <input type="file" name="thumbpix"><br>';

echo '<input type="submit" value="Uploade Thumbnail Photo">';

echo '</form>';

?>

 

This is the PHP script that handles the process:

<?php

session_start();

 

$username = $_SESSION["username"];

 

 

//This is the directory where images will be saved

$target = "Images/Directory_Thumbs/";

$target = $target . basename( $_FILES['thumbpix']['name']);

 

// This gets all the other information from the form.

$pic=($_FILES['thumbpix']['name']);

 

// Connects to your Database.

mysql_connect("test.mysite.com", "username", "passcode") or die(mysql_error()) ;

mysql_select_db("database_name") or die(mysql_error()) ;

 

// Save the information to the database for the individual username.

mysql_query("UPDATE table_name SET thumbpix = $pic WHERE username = $username");

 

 

// Upload the photo to the server.

if(move_uploaded_file($_FILES['thumbpix']['tmp_name'], $target))

{

 

//Tells you if its all ok

echo "The file ". basename( $_FILES['thumbpix']['name']). " has been uploaded, and your information has been added to the directory";

echo "

<br />

<br />

<form method=\"post\">

<input type=\"button\" value=\"Close Window\" onclick=\"window.close()\">

</form";

}

else {

 

//Gives an error if its not

echo "Sorry, there was a problem uploading your file.";

}

?>

 

 

The image does eventually get moved to the directory.  The filename, however, is nowhere to be found.  If I use this SQL:

mysql_query("INSERT INTO `table_name` VALUES ('$pic')") ;

 

...the script works.  But I need to be able to upload to a particular username.  That's why I've been trying the WHERE clause but without any success so far.  Thank you.

 

 

Hugo

Nevermind everyone.  I figured out the problem.  I forgot the single-quotes around the SQL syntax.  The following SQL command worked perfectly:

 

mysql_query("UPDATE members SET thumbpix ='$pic' WHERE username = '$username'");

 

 

Thank you.

 

 

Hugo

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.