Craigus Posted May 28, 2009 Share Posted May 28, 2009 Howdy all, I have a form that inserts some data along with an image into mysql. What I'm wanting to do it if the user leaves the image upload field blank I want it to be skipped (image not required). I've spent some time on this and am not exactly sure on the best method to use? Any pointers would be greatly appreciated. My insert code: <?php //Get DB configuraion file require('./config/config.php'); //Set variables $species = $_POST['species']; $location = $_POST['location']; $state = $_POST['state']; $date = $_POST['date']; $comments = $_POST['comments']; //Check file is a valid image and under 200kb if (($_FILES["image"]["type"] == "image/jpeg") && ($_FILES["image"]["size"] < 200000)) { //prepare the image for insertion $image =addslashes (file_get_contents($_FILES['image']['tmp_name'])); //Insert into database $sql="INSERT INTO bird_db (image, species, location, state, date, latlong, comments) VALUES ('$image', '$species', '$location', '$state', '$date', '$latlong', '$comments')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } //Results echo "Thank you, your record has been added to the database"; } //Error if file invalid type/size else {echo "Sorry, your image is more then 200KB or you have entered an invalid file type (.jpg only)";} ?> Quote Link to comment Share on other sites More sharing options...
anupamsaha Posted May 28, 2009 Share Posted May 28, 2009 Please check the "temp" upload location of the file. If it is not an image or a blank, then don't record the image. Does it make sense? Quote Link to comment Share on other sites More sharing options...
Craigus Posted May 28, 2009 Author Share Posted May 28, 2009 Not 100%? :-\ Quote Link to comment Share on other sites More sharing options...
Dathremar Posted May 28, 2009 Share Posted May 28, 2009 <?php //Get DB configuraion file require('./config/config.php'); //Set variables $species = $_POST['species']; $location = $_POST['location']; $state = $_POST['state']; $date = $_POST['date']; $comments = $_POST['comments']; if (isset($_FILES['image']['tmp_name'])) { //Check file is a valid image and under 200kb if (($_FILES["image"]["type"] == "image/jpeg") && ($_FILES["image"]["size"] < 200000)) { //prepare the image for insertion $image =addslashes (file_get_contents($_FILES['image']['tmp_name'])); } else echo "Sorry, your image is more then 200KB or you have entered an invalid file type (.jpg only)"; } else $image = 'NULL'; //Insert into database $sql="INSERT INTO bird_db (image, species, location, state, date, latlong, comments) VALUES ('$image', '$species', '$location', '$state', '$date', '$latlong', '$comments')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } else echo "Thank you, your record has been added to the database"; } ?> Try this. P.S Not tested for errors Quote Link to comment Share on other sites More sharing options...
Craigus Posted May 28, 2009 Author Share Posted May 28, 2009 Thanks Dathremar, I'll give it a go. Cheers. Quote Link to comment Share on other sites More sharing options...
waynew Posted May 28, 2009 Share Posted May 28, 2009 Always use: is_uploaded_file() when dealing with file uploads. Quote Link to comment Share on other sites More sharing options...
waynew Posted May 28, 2009 Share Posted May 28, 2009 <?php //Get DB configuraion file require('./config/config.php'); //Set variables $species = mysql_real_escape_string($_POST['species']); $location = mysql_real_escape_string($_POST['location']); $state = mysql_real_escape_string($_POST['state']); $date = mysql_real_escape_string($_POST['date']); $comments = mysql_real_escape_string($_POST['comments']); if (is_uploaded_file($_FILES['image']['tmp_name'])){ //Check file is a valid image and under 200kb if (($_FILES["image"]["type"] == "image/jpeg") && ($_FILES["image"]["size"] < 200000)){ //prepare the image for insertion $image =mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name'])); } else{ echo "Sorry, your image is more then 200KB or you have entered an invalid file type (.jpg only)"; } } else{ $image = 'NULL'; } //Insert into database $sql="INSERT INTO bird_db (image, species, location, state, date, latlong, comments) VALUES ('$image', '$species', '$location', '$state', '$date', '$latlong', '$comments')"; $insertion = mysql_query($sql) or trigger_error(mysql_error()); if($insertion){ echo "Thank you, your record has been added to the database"; } ?> Quote Link to comment Share on other sites More sharing options...
Craigus Posted May 28, 2009 Author Share Posted May 28, 2009 Hey waynewex, that works dude, thanks heaps! Quote Link to comment 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.