AscIId Posted June 6, 2007 Share Posted June 6, 2007 Hi guys, this is my first post here but I am not a newbie What am I trying to do? I am trying to upload an Image to a server along with a record set of the image's data. How am I going to do it? Connection to a MyISAM MySQL database via siteroot/includes/database/connect.php. Fields in database: sID (PK autoindex) txtPerson - submitters name (allow null) txtComments - comments about the image imgMainpic - for the picture A form on siteroot/submit.php action="includes/submit/submit.php". on includes/submit/submit.php ####################################################### #© AscIId 2007 This file MUST NOT be distrubuted or copied in ANY WAY AT ALL # # # #This file submits data to the database # ######################################################## ##### ///// connect to db \\\\\ includeonce ("../database/connect.php"); ##### ///// retreve data from form \\\\\ #Person Name From Form $txtPerson = $_POST["txtPerson"] #Comments from Form $txtComments = $_POST["txtComments"] #Image from form $imgMainpic = $_POST["imgMainpic"] #more fields to added here later (not listed here because of security) ############ <image stuff goes here> ############## $query = "INSERT INTO tblSightings VALUES('NULL',$txtPerson, $txtComments, $imgMainpic, $blah)"; $result = mysql_query($query); if(mysql_affected_rows() > 0){ if($debug==true){ echo ("Record Uploaded"); } } } else { if($debug==true){ echo ("Record Upload Failed"); } die ("There was an error in submission, please try again later " . $mysql_error() ; } What i need help with is uploading the image to the database. Could this be done by changing the image to a binary BLOB? Please give me some code examples I would find that really helpful. AscIId Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/ Share on other sites More sharing options...
gabeg Posted June 6, 2007 Share Posted June 6, 2007 Is there any reason why you want to keep the image in the database? Why not just have an uploads directory, and track the file name for the corresponding image in the database? Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-269588 Share on other sites More sharing options...
kael.shipman Posted June 6, 2007 Share Posted June 6, 2007 I wrote this out while gabeg was replying, too, so I might as well still post it.... Maybe I've always gone about this incorrectly, but instead of burdening the database with your image files, I would just move the uploaded file into a folder with a name that corresponds to a database entry for its information. Example: <?php ///after user submits entry and all entries are validated @mysql_query("INSERT INTO tblSightings VALUES('NULL',$txtPerson, $txtComments, $blah)"); //run your query checks, then get an id if it worked $photoName = @mysql_insert_id(); //use the insert ID as the photo name in a specific folder on your site $newDest = '/path/to/special/folder/'.$photoName; move_uploaded_file($_FILES["uploadImg"]["tmp_name"],$newDest); ?> Just a quick example; I hope you get the gist, anyway. Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-269589 Share on other sites More sharing options...
AscIId Posted June 7, 2007 Author Share Posted June 7, 2007 thanks... i have inserted the code and i think it works.... I just need to do a mysqladmin flush-hosts because i had a typo on the password and i didnt think to check it Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-269797 Share on other sites More sharing options...
AscIId Posted June 7, 2007 Author Share Posted June 7, 2007 ok i have tested the script and it doesnt work Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270154 Share on other sites More sharing options...
chocopi Posted June 7, 2007 Share Posted June 7, 2007 Well why dont you post the error messages then Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270155 Share on other sites More sharing options...
AscIId Posted June 7, 2007 Author Share Posted June 7, 2007 yea, that would be helpful it can connect to the DB, no problem... however when i do @mysql_query ("INSERT INTO tblSightings VALUES('NULL', $blah,$blah,$postComments,$blah)"); it does not affect any rows in the table... without the image script, i need this to work before the image script can be used :S ??? Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270249 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 The @ supresses errors, which is why it is not working because it is throwing an error @mysql_query ("INSERT INTO tblSightings VALUES(NULL, '$blah','$blah','$postComments','$blah')") OR DIE(mysql_error()); NULL should not be encapsulated in single quotes. Which chances are is where your error comes from that and not having single quotes around the values. Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270255 Share on other sites More sharing options...
AscIId Posted June 7, 2007 Author Share Posted June 7, 2007 it doesnt throw any errors, even without the @. but there are still 0 affected rows Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270282 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Weird man, have you tried listing the column names out? mysql_query ("INSERT INTO tblSightings (`col1`, `col1`, `col3) VALUES(NULL, '$blah','$blah','$postComments','$blah')") OR DIE(mysql_error()); And seeing if that works? Also have you verified in your database that no rows are being entered? Mysql_affected may only apply to update/selects I am unsure. Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270286 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Share Posted June 7, 2007 i would suggest you save the file via the filesystem (ie. as an actual image file) and store the FILENAME in the database. in this case, it's a matter of setting up its filename, ensuring it's unique, moving the temp file to its final location (move_uploaded_file()), and inserting its entry into the database. have a look in the manual regarding handling file uploads; there's an entire section on it. should give you some good background. if you wanted to save the file as its binary format, you'd need a BLOB column in your table. i've yet to see a situation where this is preferable, though. Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270290 Share on other sites More sharing options...
AscIId Posted June 7, 2007 Author Share Posted June 7, 2007 ... i am not there yet. i am still having troubles uploading the accomplying data. and i am using column names already and it isnt throwing any errors at me! Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270314 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Share Posted June 7, 2007 ... i am not there yet. i am still having troubles uploading the accomplying data. and i am using column names already and it isnt throwing any errors at me! Explain to me in detail what isn't working. Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270318 Share on other sites More sharing options...
AscIId Posted June 7, 2007 Author Share Posted June 7, 2007 FORGET THE IMAGE UPLOAD, i am still trying to upload the text that should go with the image adding some records to the SQL database.... if i type into my form "blah" in all the fields, (not including the upload image field) and submit the data the data does not appear in the database (no rows added) even though there are no errors thrown. Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270324 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Share Posted June 7, 2007 FORGET THE IMAGE UPLOAD, i am still trying to upload the text that should go with the image adding some records to the SQL database.... if i type into my form "blah" in all the fields, (not including the upload image field) and submit the data the data does not appear in the database (no rows added) even though there are no errors thrown. attach your file or files please! Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270329 Share on other sites More sharing options...
AscIId Posted June 7, 2007 Author Share Posted June 7, 2007 tomorrow got to go now Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270333 Share on other sites More sharing options...
AscIId Posted June 8, 2007 Author Share Posted June 8, 2007 $file = $_Post["carImage"]; $blah= $_Post["blah"]; $postComments = $_POST["postComments"]; #################### # connect.php #################### $hostname_mshe = "localhost"; $database_mshe = //removed $username_mshe = //removed $password_mshe = //removed $mshame = mysql_connect($hostname_mshe, $username_mshe, $password_mshe) or die ("Unable to connect to DB"); ########## #submit.php ########## mysql_select_db("mshe", $mshe); $query = ("INSERT INTO `tblSightings` ( `sID` , `blah` , `blah` , `postComments` ) VALUES ( NULL, '$blah','$blah')" OR DIE("Somehing went wrong " . mysql_error())); #$carImage = @mysql_insert_id(); //use the insert ID as the photo name in a specific folder on your site #$newDest = 'xxxxxxxxxxxxxxxxxx/uploadpics/'.$carImage; #move_uploaded_file($_FILES["uploadImg"]["tmp_name"],$newDest); $result = mysql_query($query); if(mysql_affected_rows() > 0){ echo "Image has been inserted succesfully"; } else { echo "Image can not be inserted check your submission"; } ############## #disconnect.php ############## mysql_close ($mshe); that is my code Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270576 Share on other sites More sharing options...
gabeg Posted June 8, 2007 Share Posted June 8, 2007 Does it work or what's the problem with it? Quote Link to comment https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/#findComment-270874 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.