emediastudios Posted September 25, 2007 Share Posted September 25, 2007 Hi all. I have a code that works fine except for one thing, it only uploads (1) image and i want to upload (4). the code i have does all ehat i want and inserts the file name in to my SQL database along with some other info. Can someone tell me how to alter my code to upload (4)images, and record there image name into sql. the code i have below i altered to process (3) pics but it doesnt upload the images and only inserts there image file name into sql. Thanks so much........... Heres the code i have <?php //This is the directory where images will be saved $target = "../images/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['name']; $suburb=$_POST['suburb']; $price=$_POST['price']; $content=$_POST['content']; $pic=($_FILES['photo']['name']); $pic2=($_FILES['photo2']['name']); $pic3=($_FILES['photo3']['name']); // Connects to your Database mysql_connect("localhost", "root", "5050888202") or die(mysql_error()) ; mysql_select_db("gcproperty") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$pic', '$pic2', '$pic3')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) --------------My problem needs to sorted here somewhere i think. { //Tells you if its all ok print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?>[/color] Quote Link to comment Share on other sites More sharing options...
Aeglos Posted September 25, 2007 Share Posted September 25, 2007 Evidently it's there, since you only call move_uploaded_file() with 'photo' and never with 'photo2' and 'photo3'. Try: <?php if ( move_uploaded_file($_FILES['photo']['tmp_name'], $target) && move_uploaded_file($_FILES['photo2']['tmp_name'], $target) && move_uploaded_file($_FILES['photo3']['tmp_name'], $target) ) { ...rest of your code... ?> That way you check for the three uploads, if one fails, all fails. Quote Link to comment Share on other sites More sharing options...
emediastudios Posted September 25, 2007 Author Share Posted September 25, 2007 Thanks, i gave it a go, still inserts all the image names and other info but only uploads, the first (photo) Doesnt upload the other images to the directory (photo1 and photo2) This is what im using now ------------------------------------------------------------------------ <?php //This is the directory where images will be saved $target = "../images/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['name']; $suburb=$_POST['suburb']; $price=$_POST['price']; $content=$_POST['content']; $photo=($_FILES['photo']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); // Connects to your Database mysql_connect("localhost", "root", "5050888202") or die(mysql_error()) ; mysql_select_db("gcproperty") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$photo', '$photo2', '$photo3')") ; //Writes the photo to the server if ( move_uploaded_file($_FILES['photo']['tmp_name'], $target) && move_uploaded_file($_FILES['photo2']['tmp_name'], $target) && move_uploaded_file($_FILES['photo3']['tmp_name'], $target) ) { //Tells you if its all ok print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
Aeglos Posted September 25, 2007 Share Posted September 25, 2007 Blah, silly me, forgot an important part. You're basically saving the three photos to the same file, Try this at the beggining of your code: <?php $target = "../images/"; $target1 = $target . basename( $_FILES['photo']['name']); $target2 = $target . basename( $_FILES['photo1']['name']); $target3 = $target . basename( $_FILES['photo2']['name']); ?> Then at the if(): <?php if ( move_uploaded_file($_FILES['photo']['tmp_name'], $target1) && move_uploaded_file($_FILES['photo2']['tmp_name'], $target2) && move_uploaded_file($_FILES['photo3']['tmp_name'], $target3) ) { ...etc... ?> 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.