bbg5000 Posted February 7, 2014 Share Posted February 7, 2014 (edited) I'm trying to add an image name from an uploaded image to MySQL database. The code for the upload works, however, when I inserted the code to add the image name to SB, it now just shows a blank page and no image is uploaded or added to the database. Can someone have a look at my code and tell me what is wrong. Thanks. <?php $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["image"]["name"]); $extension = end($temp); if ((($_FILES["image"]["type"] == "image/gif") || ($_FILES["image"]["type"] == "image/jpeg") || ($_FILES["image"]["type"] == "image/jpg") || ($_FILES["image"]["type"] == "image/pjpeg") || ($_FILES["image"]["type"] == "image/x-png") || ($_FILES["image"]["type"] == "image/png")) && ($_FILES["image"]["size"] < 200000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["image"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["image"]["name"] . "<br>"; echo "Type: " . $_FILES["image"]["type"] . "<br>"; echo "Size: " . ($_FILES["image"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_FILES["image"]["name"])) { echo $_FILES["image"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["image"]["tmp_name"], "upload/" . $_FILES["image"]["name"]); echo "Stored in: " . "upload/" . $_FILES["image"]["name"]; if(move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]); { $con=mysqli_connect("localhost","uname","***","dbname"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $photo = $_FILES['image']['name']; mysqli_query($con,"INSERT INTO image (image) VALUES ('$photo')"); mysqli_close($con); else { echo 'File name not stored in database'; } } } } } else { echo "Invalid file"; } ?> Edited February 7, 2014 by bbg5000 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 7, 2014 Share Posted February 7, 2014 (edited) When the file is being moved ones php delete it from tmp folder! You're calling move_uploaded_file() functions twice. Edited February 7, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
bbg5000 Posted February 7, 2014 Author Share Posted February 7, 2014 (edited) I've changed the code up a little bit. It now uploads the image, yet it still wont add it to the database. Any ideas? <?php $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["image"]["name"]); $extension = end($temp); $photo = $_FILES['image']['name']; $con=mysqli_connect("localhost","acro","2313canadian","acropm"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_query($con,"INSERT INTO image (image) VALUES ($photo)"); mysqli_close($con); if ((($_FILES["image"]["type"] == "image/gif") || ($_FILES["image"]["type"] == "image/jpeg") || ($_FILES["image"]["type"] == "image/jpg") || ($_FILES["image"]["type"] == "image/pjpeg") || ($_FILES["image"]["type"] == "image/x-png") || ($_FILES["image"]["type"] == "image/png")) && ($_FILES["image"]["size"] < 200000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["image"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["image"]["name"] . "<br>"; echo "Type: " . $_FILES["image"]["type"] . "<br>"; echo "Size: " . ($_FILES["image"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_FILES["image"]["name"])) { echo $_FILES["image"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["image"]["tmp_name"], "upload/" . $_FILES["image"]["name"]); echo "Stored in: " . "upload/" . $_FILES["image"]["name"]; } } } else { echo "Invalid file"; } ?> Edited February 7, 2014 by bbg5000 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 7, 2014 Share Posted February 7, 2014 Use mysqli_error() function to debug your query. Quote Link to comment Share on other sites More sharing options...
bbg5000 Posted February 7, 2014 Author Share Posted February 7, 2014 Added this line and it worked. mysqli_query($con,"INSERT INTO image (image) VALUES ('". $photo . "')") or die(mysqli_error($con)); Thanks for your help. 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.