craiglaw2504 Posted December 9, 2009 Share Posted December 9, 2009 Morning Guys, I have made a simple html form that uses an insert.php to input data in to my mysql DB (that is used for my search). At the moment the form only consists of text fields, however I need to updload an image and a PDF to the record (individual columns have been created). I basicly want to upload the two items to the server and simply store the file path in the database....... problem is I have no ide how to do this short of manualy uploading the two and manualy typing the filepaths into the mysql DB. Can someone please help me out?! Code so far (stripped down to basic's):- [uHTML ]Form[/u] <form action="insert.php" method="post"> Project Number: <input type="text" name="projectnumber" /> Property: <input type="text" name="property" /> Address: <input type="text" name="address" /> City: <input type="text" name="city" /> Post Code: <input type="text" name="postcode" /> <input type="submit" /> </form> insert.php $sql="INSERT INTO database_name (projectnumber, property, address, city, postcode) VALUES ('$_POST[projectnumber]','$_POST[property]','$_POST[address]','$_POST[city]','$_POST[postcode]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> This needs to be upload at the same time as the new record is inserted so that it gets matched up witht the right property. Would really appreciate help on this as I have scoured the net for hours and short of purchasing a program (which doesn't teach jack) can figure it out. Thanks again, in advance. Kuga Link to comment https://forums.phpfreaks.com/topic/184509-php-new-record-form/ Share on other sites More sharing options...
Goldeneye Posted December 9, 2009 Share Posted December 9, 2009 So, you want to learn how to upload files using PHP? First, you have to include enctype="multipart/form-data" in your form action like so: <form enctype="multipart/form-data"> ... </form> Next, you need to add fields to your form which allows the user to select a file for upload. You can have more than one of these between <form>*</form> tags: <input name="image_file" type="file"> <input name="pdf_file" type="file"> After that, you need to verify the files using a PHP script. Things to consider and look for: the file-size of the uploaded files -- you don't want your users uploading 2-GB PDF-file, do you? it is recommended to rename the files in some way (using a date-time scheme) so you don't have to worry about users uploading files with the same name. the file-extension of the file -- you don't want users to upload EXE-files when they are supposed to upload PDF-files for JPG-files make a table in your database and create fields to store the file-size, file-name (that you renamed the file to), optionally: the date the file was uploaded, the file-type (it'd be easier to just include this in the file-name field). If you plan to store these files in different directories, you should store the complete path in the file-name field (ex: /path/to/uploaded/file/foobar.pdf) And create an auto-incremented column so you can access your files using an ID as opposed to a filename. ex: download.php?file=1 instead of download.php?file=foobar.pdf Sorry, I can't provide some example-code snippets at the moment. Link to comment https://forums.phpfreaks.com/topic/184509-php-new-record-form/#findComment-974047 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.