martinm92 Posted July 12, 2012 Share Posted July 12, 2012 Well I wanted to avoid help for as long as I could but I have something going wrong that isn't uploading to my db however the script is working because it uploads the pictures to my server. Think I just need another pair of eyes.. if($_SERVER['REQUEST_METHOD'] == 'POST'){ if(($_FILES['file']['type'] == 'image/gif') || ($_FILES['file']['type'] == 'image/jpeg') || ($_FILES['file']['type'] == 'image/png')){ if($_FILES['type']['error'] > 0){ echo "Return Code: " . $_FILES['file']['error'] . "<br>"; }else{ $title = $_POST['title']; $url = $_POST['url']; $wwd = $_POST['wwd']; $otherfeat = $_POST['otherfeat']; $original = $_FILES['file']['name']; $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); $newfiles = md5(time()) . "." . $ext; if(move_uploaded_file($_FILES['file']['tmp_name'], "./upload/".$original)){ mysql_query("INSERT INTO `portfolio` (`title`, `webadd`, `whatwedid`, `otherfeat`, `img_org`, `img_n`) VALUES ('".$title."', '".$url."', '".$wwd."', '".$otherfeat."', '".$$original."', '".$newfiles."')"); include ("image_resize.php"); echo "<p>Upload successful!</p>"; }else{ echo "<p>Failed to upload!</p>"; } } }else{ echo "<p>Invalid file type!</p>"; } } Here is my form aswell if you want to see that. <form name="uploader" method="POST" enctype="multipart/form-data"> <p>Title:</p> <p><input type="text" name="title" class="addportinput" /></p> <p>Website URL:</p> <p><input type="text" name="url" class="addportinput" /></p> <p>What We Did:</p> <p><textarea name="wwd" class="addportlongt"></textarea></p> <p>Other Features:</p> <p><textarea name="otherfeat" class="addportlongt"></textarea></p> <p>Screenshot:</p> <p><input type="file" name="file" class="addportimage" /></p> <p><input type="submit" class="submitButton" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/ Share on other sites More sharing options...
Psycho Posted July 12, 2012 Share Posted July 12, 2012 Most likely your query is failing. You have no error handling so you won't see the errors if they occur. if(move_uploaded_file($_FILES['file']['tmp_name'], "./upload/".$original)) { $query = "INSERT INTO `portfolio` (`title`, `webadd`, `whatwedid`, `otherfeat`, `img_org`, `img_n`) VALUES ('$title', '$url', '$wwd', '$otherfeat', '$$original', '$newfiles')"; $result = mysql_query($query); if(!$result) { echo "Query Failed!<br>Query: $query<br>Error: " . mysql_error(); } include ("image_resize.php"); echo "<p>Upload successful!</p>"; } Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361065 Share on other sites More sharing options...
andrew_biggart Posted July 12, 2012 Share Posted July 12, 2012 I constantly come across this problem. Double check your variable names in your code and your query. Also double check you table and column names are correct. Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361069 Share on other sites More sharing options...
martinm92 Posted July 12, 2012 Author Share Posted July 12, 2012 Well it has now entered into the db however now when i try it again it isn't working again.. :/ So it has created 1 entry but now it won't enter them again.. Also I am getting no mysql errors. Im pretty confused. Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361075 Share on other sites More sharing options...
andrew_biggart Posted July 12, 2012 Share Posted July 12, 2012 Have you got error reporting on? Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361076 Share on other sites More sharing options...
martinm92 Posted July 12, 2012 Author Share Posted July 12, 2012 ye error reporting is on, but like i said it has entered stuff into the db once.. Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361088 Share on other sites More sharing options...
andrew_biggart Posted July 12, 2012 Share Posted July 12, 2012 Have you added error handling for the sql like Psycho suggested? Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361100 Share on other sites More sharing options...
Psycho Posted July 12, 2012 Share Posted July 12, 2012 Did you notice that I created the query as a string variable first, then used that variable in the actual mysql_query() function? If the query is not producing errors but does not produce the results you want then echo the query to the page to verify if it is is built how you think it should be. In fact you should ALWAYS verify the content of dynamically created queries. Doesn't just build a query and run it and hope it works as you think. Echo the query out before even trying to run it. I can see one glaring problem in your query as you built it. Look at the value you are inserting for the `img_org` field '$$original' I'm pretty sure you didn't mean to use a variable variable (i.e. the double dollar sign). Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361112 Share on other sites More sharing options...
martinm92 Posted July 13, 2012 Author Share Posted July 13, 2012 Ok well since i have changed $$original it is working now haha... I new it was something simple Thanks Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361187 Share on other sites More sharing options...
martinm92 Posted July 13, 2012 Author Share Posted July 13, 2012 Is spoke to soon I just got this error.. Query Failed! Query: INSERT INTO `portfolio` (`title`, `webadd`, `whatwedid`, `otherfeat`, `img_org`, `img_n`) VALUES ('Steppin' Out', 'http://www.steppinout.me.uk/', 'PSD Design, HTML/CSS', 'We gave this website a photogallery and an admin login to allow the owner to update the photogallery.', '$stepout.jpg', 'efd897171bdd5796a3083109f35bd2db.jpg') Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Out', 'http://www.steppinout.me.uk/', 'PSD Design, HTML/CSS', 'We gave this webs' at line 4 Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361189 Share on other sites More sharing options...
PFMaBiSmAd Posted July 13, 2012 Share Posted July 13, 2012 All string data that is put into a query statement that might contain sql special characters must be escaped so that the special sql characters in the data don't break the sql syntax. See this link - mysql_real_escape_string Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361194 Share on other sites More sharing options...
martinm92 Posted July 13, 2012 Author Share Posted July 13, 2012 Oh yes of course I have been using ' in my text inputs... Thanks guys thats fixed now, tested it a few times and its working perfectly Quote Link to comment https://forums.phpfreaks.com/topic/265571-wont-upload-to-db/#findComment-1361291 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.