richardmlaws Posted March 3, 2006 Share Posted March 3, 2006 Hi, I am having trouble uploading some text and an image to a MYSQL database via a form. I can upload the text fine but "no go" with the image. I am not a great PHP coder so I wouldn't be suprised if this is all wrong. Any help would be greatly appreciated.I'm not too worried about security at the moment thats why I have got the connection settings in there.Here is the code I have been using...=============================================================<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body><? if($_POST['submit']){ mysql_connect("host","user","pass"); mysql_select_db("db") OR DIE ("Unable to select db".mysql_error()); $id = $_POST['id']; $description = $_POST['description']; $date = $_POST['date']; $imgData = addslashes(fread(fopen($_FILES['userfile']['tmp_name'], "r"), filesize($_FILES['userfile']['tmp_name']))) or die("Could not open file."); $result=MYSQL_QUERY("INSERT INTO table (id, description, date, image, filename, filesize, filetype)". "VALUES ('$id','$description','$date','$imgData','$_FILES['image']['filename']','$_FILES['image']['filesize']','$_FILES['image']['filetype']')"); }?><font face="verdana"><h3><center><u>Add Equipment Record</u></center</h3><form enctype="multipart/form-data" action="" method="POST"><TABLE WIDTH="100%" CELLPADDING="10" CELLSPACING="0" BORDER="2" RULES=NONE FRAME=BOX BGCOLOR="#FAEBD7"><col span="1" align="right"><TR> <TD><font face="verdana"><b>Equipment ID:</b></font></TD> <TD><INPUT TYPE='TEXT' NAME='id' VALUE='' size=60></TD></TR><TR> <TD><font face="verdana"><b>Description:</b></font></TD> <TD><INPUT TYPE='TEXT' NAME='description' VALUE='' size=60></TD></TR><br><TR> <TD><font face="verdana"><b>Date:</b></font><br><font face="verdana" size="1"><left>(yyyy-mm-dd)</left></font></TD> <TD> <INPUT TYPE='TEXT' NAME='date' size=60> </TD></TR><tr> <td><font face="verdana"><b>Image:</b></font></td> <td><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile[]" type="file" class="box" id="userfile"></td></tr><tr><td> </td><td><input name="upload" type="submit" id="upload" value="Submit"></td></tr></TABLE></form></font></body></html>=============================================================Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/4041-upload-image-and-text-via-form/ Share on other sites More sharing options...
michaellunsford Posted March 4, 2006 Share Posted March 4, 2006 [!--quoteo(post=351444:date=Mar 3 2006, 04:25 PM:name=richardmlaws)--][div class=\'quotetop\']QUOTE(richardmlaws @ Mar 3 2006, 04:25 PM) [snapback]351444[/snapback][/div][div class=\'quotemain\'][!--quotec--] $imgData = addslashes(fread(fopen($_FILES['userfile']['tmp_name'], "r"),[/quote]try$imgData = mysql_real_escape_string(file_get_contents($_FILES['userfile']['tmp_name']));Also, you're using blob, right? Quote Link to comment https://forums.phpfreaks.com/topic/4041-upload-image-and-text-via-form/#findComment-14044 Share on other sites More sharing options...
richardmlaws Posted March 4, 2006 Author Share Posted March 4, 2006 hi michael thanks for the reply but unfortunately its still not working.my table has the following attributes and data types.id - int(5)description -varchar(100) date - dateimage - longblob filename - varchar(50) filesize - int(10) filetype - varchar(50)My PHP is looking like this now...==============================================================<? if($_POST['submit']){ mysql_connect("host","user","pass"); mysql_select_db("db") OR DIE ("Unable to select db".mysql_error()); $id = $_POST['id']; $description = $_POST['description']; $date = $_POST['date']; $imgData = mysql_real_escape_string(file_get_contents($_FILES['userfile']['tmp_name'])), filesize($_FILES['userfile']['tmp_name']))) or die("Could not open file."); $result=MYSQL_QUERY("INSERT INTO table (id, description, date, image, filename, filesize, filetype)". "VALUES ('$id','$description','$date','$imgData','$_FILES['image']['filename']','$_FILES['image']['filesize']','$_FILES['image']['filetype']')");}?>==============================================================Thanks Quote Link to comment https://forums.phpfreaks.com/topic/4041-upload-image-and-text-via-form/#findComment-14145 Share on other sites More sharing options...
richardmlaws Posted March 4, 2006 Author Share Posted March 4, 2006 solved...[a href=\"http://www.scit.wlv.ac.uk/~jphb/sst/php/extra/image1.html\" target=\"_blank\"]http://www.scit.wlv.ac.uk/~jphb/sst/php/extra/image1.html[/a] Quote Link to comment https://forums.phpfreaks.com/topic/4041-upload-image-and-text-via-form/#findComment-14261 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.