I've looked all over and have basically found the same code for this issue, but it doesn't work for me. I'm not a real programmer, more of a graphic artist who has had to learn stuff, so it is hard to figure this out.
I have a form. In the form there is an upload file code. I want to have the user click that, select the image he wants to put on the site (in a folder called 'pix') and have the name of the file written to my database (into a field called 'photo').
I simply can not figure out how to make the code I keep finding all over work for me. I rely on Dreamweaver for a lot of my PHP. Anyway, here is the entire page. How do I do this? I'm pulling my hair out.
when i fill out the form and select the image to upload it IS uploading that image. What i need now is for it to write the name of the image in the database for later reference. It is driving me crazy. I have tried half a dozen techniques (all of which are very similar) but no joy. Here is the working code for the add.php page, as well as the update.php, minus the ability for it write the name to the DB. If you could tell me how to make it work i would be very grateful. Please help a brother out! =D
Sorry for the delay in my response. Usually i get an email saying a post has been answered but not this time. Anyway, yes, it is writing all the data to the database except for the name of the file it is uploading. so the blogID, date, title, content all upload to the database. photo just comes up NULL. I have tried several techniques I got off the web but I don't have enough php understanding to apply it to my code.
i don't know what that means and i don't know what to change. im not a total novice but I do rely on DW to write a lot of the PHP code. What am I supposed to change to make it work?
criterion9,
I know what a variable is. I understand that. But you are coming at the problem from the mindset of a programmer, and I'm coming at it from the mind of an artist. Want me to show you how to be creative? Fine, here, I'll use your technique...'think differently'. There you go. I've helped you.
when i do 'echo $pic;' i get no result. so i take that as telling me that I am not filling those variables at all, which is strange because 'photo' definitly exists.
Last edited by mightyhokie; 10-23-2012 at 09:41 AM.
okay, here is the 'upload a file and write to mySQL' code that I have been trying to fuse with my current code. My current code WORKS in the sense that it writes all the data (blogID, date, content, title) to the correct fields in the database. it does upload the file to the correct folder. id does NOT write the name of the uploaded file to the database.
<?php
//This is the directory where images will be saved
$target = "pix/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$pic=($_FILES['photo']['name']);
//Writes the information to the database
mysql_query("INSERT INTO `base` VALUES ( '$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
when i apply this code, it still writes everything to the database EXCEPT the name of the uploaded file (and the uploaded file is still being uploaded properly).
what am i missing?
Is the file showing up in the folder with the correct name as expected?
Looks like you need to add your name field into this query so it will update as expected as well as moving your upload check/name variable assignment above your query:
$updateSQL = sprintf("UPDATE main SET title=%s, content=%s, photo=%s, `date`=%s WHERE blogID=%s",
GetSQLValueString($_POST['title'], "text"),
GetSQLValueString($_POST['content'], "text"),
GetSQLValueString($_POST['photo'], "text"),
GetSQLValueString($_POST['date'], "text"),
GetSQLValueString($_POST['blogID'], "int"));
Basically a very similar thing needs to happen based on your add script. You need to add the name field into your query and move your file upload/name variable assignment portion above the query so it can be included:
Bookmarks