2

Hi at all i got a problem i got a form which uploads files and insert into mysql the mysql insert part is working but he only moves one file of the three to the server need please some help here is my code. The Upload Form:

<form enctype="multipart/form-data" action="add-exposes.php" method="POST">  Expose Name: <input type="text" name="name"><br>  Expose Beschreibung: <textarea style="width: 810px; height: 200px" name="expose_desc" class="textarea"></textarea><br>  Expose Kategorie:<select name="expose_cat" size="3"> 
                        <?php 
                    mysql_connect("xxx.de.mysql", "xxxx", "xxx") or die(mysql_error()) ;  mysql_select_db("handwerker_verb") or die(mysql_error()) ;

                    $data = mysql_query("SELECT * FROM cats") or die(mysql_error());
                    while($info = mysql_fetch_array( $data )) {
                    echo '<option value="'.$info['id'].'">'.$info['cat'].'</option>';
                    } ?>


                         </select><!--<input type="text" name="expose_cat">--><br>Expose Preis:<input type="text" name="price"> <br> Bild: <input type="file" name="photo"><br> <input type="file" name="photo1"><br> <input type="file" name="photo2"><br>   <input type="submit" value="Add">  </form>

Add to server and mysql:

<?php   $target = "images/";  $target = $target . basename( $_FILES['photo']['name']);
$target1 = "images/";  $target1 = $target1 . basename( $_FILES['photo1']['name']);
$target2 = "images/";  $target2 = $target2 . basename( $_FILES['photo2']['name']);

        $name=$_POST['name'];  $expose_desc=$_POST['expose_desc'];  $expose_cat=$_POST['expose_cat'];  $pic=($_FILES['photo']['name']);  $price=$_POST['price']; $pic1=($_FILES['photo1']['name']); $pic2=($_FILES['photo2']['name']); 

        mysql_connect("xxxx.de.mysql", "xx", "xxx") or die(mysql_error()) ;  mysql_select_db("handwerker_verb") or die(mysql_error()) ;

          mysql_query("INSERT INTO `exposes` VALUES ('', '$name', '$pic', '$expose_desc', '$expose_cat', '$price', '$pic1', '$pic2' )") ; 

          if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  {   echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  } 
          elseif(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1))  {   echo "The 2 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  } 
          elseif(move_uploaded_file($_FILES['photo2']['tmp_name'], $target2))  {   echo "The 3 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  }

           else {    echo "Sorry, there was a problem uploading your file.";  }  ?>                         
4
  • 2
    Only one file will be uploaded because you have if-elseif-elseif, change that to if-if-if. Commented Oct 24, 2014 at 11:28
  • thanks will gie it atry give you feedback in minute Commented Oct 24, 2014 at 11:29
  • awsome thanks working now Commented Oct 24, 2014 at 11:35
  • I added my solution as answer, please mark it :) Commented Oct 24, 2014 at 11:36

1 Answer 1

1

move_uploaded_file returns true on success, the remaining elseif are never reached. Try something like this:

$error = false;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  
{   
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
} else $error = true;

if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1))  
{   
echo "The 2 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
} else $error = true; 

if(move_uploaded_file($_FILES['photo2']['tmp_name'], $target2))  
{   
echo "The 3 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
} else $error = true;

if($error)
   echo "Sorry, there was a problem uploading your file.";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.