Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Uploading multiple images with a form to server and storing file name in MySQL

    I have a form that allows the user to upload multiple images (5 max). This code works for me for uploading one image, but it doesn't work now that I'm trying to upload multiple images.

    It uploads the file names to the MySQL table perfectly, but it doesn't upload the image to the server. Can anyone tell me where I've gone wrong -- thanks!!! :)
    I'm assuming its something small I'm missing since it worked for uploading just one image. ;)

    Here is my form:

    Code:
    <form action="nextpage.php" method="post" enctype="multipart/form-data">
    
    <table>
    <tr>
    <td valign="top">Upload Image: </td>
    
    <td>
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo" type="file" size="30"><br />
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo2" type="file" size="30"><br />
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo3" type="file" size="30"><br />
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo4" type="file" size="30"><br />
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo5" type="file" size="30"></td>
    </tr>
    </table>
    Here is the nextpage.php code:

    Code:
    <?php
    $photo=($_FILES['photo']['name']);
    $photo2=($_FILES['photo2']['name']);
    $photo3=($_FILES['photo3']['name']);
    $photo4=($_FILES['photo4']['name']);
    $photo5=($_FILES['photo5']['name']);
    
    //This is the directory where images will be saved
    $target = "images/uploaded/";
    $target = $target . basename( $_FILES['photo']['name']['photo2']['name']['photo3']['name']['photo4']['name']['photo5']['name']);
    
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo']['tmp_name']['photo2']['tmp_name']['photo3']['tmp_name']['photo4']['tmp_name']['photo5']['tmp_name'], $target))
    {
    
    //Tells you if its all ok
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
    }
    else {
    
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
    }
    
    $database info
    
    mysql_connect("$hostname", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="INSERT INTO `Listings` (`photo`, `photo2`, `photo3`, `photo4`, `photo5`) VALUES('".$photo."', '".$photo2."', '".$photo3."', '".$photo4."', '".$photo5."')";
    $result=mysql_query($sql);

  • #2
    New Coder
    Join Date
    Aug 2008
    Posts
    22
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You can't just upload all the 5 images using one line.
    Do them separately

    Code:
    move_uploaded_file($_FILES['photo']['tmp_name']['photo2']['tmp_name']['photo3']['tmp_name']['photo4']['tmp_name']['photo5']['tmp_name'], $target)

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How would I write that?

    Like this?:

    Code:
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
    if(move_uploaded_file($_FILES['photo2']['tmp_name'], $target))
    if(move_uploaded_file($_FILES['photo3']['tmp_name'], $target))
    if(move_uploaded_file($_FILES['photo4']['tmp_name'], $target))
    if(move_uploaded_file($_FILES['photo5']['tmp_name'], $target))
    {
    Do I also have to change this line?

    Code:
    $target = $target . basename( $_FILES['photo']['name']['photo2']['name']['photo3']['name']['photo4']['name']['photo5']['name']);
    Thanks!

  • #4
    New Coder
    Join Date
    Aug 2008
    Posts
    22
    Thanks
    0
    Thanked 2 Times in 2 Posts
    do it like this :

    Code:
    $target = "images/uploaded/";
    $targetx = $target . basename( $_FILES['photo']['name']);
    
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $targetx))
    {
    
    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }
    
    $targetx = $target . basename( $_FILES['photo2']['name']);
    
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo2']['tmp_name'], $targetx))
    {
    
    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }
    
    $targetx = $target . basename( $_FILES['photo3']['name']);
    
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo3']['tmp_name'], $targetx))
    {
    
    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }
    
    $targetx = $target . basename( $_FILES['photo4']['name']);
    
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo4']['tmp_name'], $targetx))
    {
    
    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }
    
    $targetx = $target . basename( $_FILES['photo5']['name']);
    
    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo5']['tmp_name'], $targetx))
    {
    
    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }

  • Users who have thanked riwan for this post:

    natie769 (03-10-2009)

  • #5
    New Coder
    Join Date
    Jan 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That worked perfectly -- thank you so much!!
    You're awesome!

  • #6
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    HTML form with PHP insert to MYSQL multiple images

    This thread has helped me a ton. Thank you!! My code is 'almost' working. I am getting this error:
    Error: Column count doesn't match value count at row 1

    I have counted the insert and MySQL table and I think I have the same everywhere so am not sure why I'm getting this error. I am posting all my related code below and would appreciate any suggestions. THANK YOU!

    HERE IS THE HTML:
    <form action="insertTEST.php" method="post" target="_top" enctype="multipart/form-data" name="modelinquiry" id="modelinquiry" onSubmit="MM_validateForm('firstname','','R', 'email','','RisEmail');return document.MM_returnValue">
    <p>Name:
    <input type="text" name="firstname" size="65"/>

    </p>
    <p> Email:
    <input type="text" name="email" size="50" />
    <br />
    <span class="red"><br />

    OPTIONAL BUT HELPFUL:</span><br />
    <br />

    Age:
    <input type="text" name="age" size="12"/>
    <br />
    <br />
    What is your state of residence?
    <input type="text" name="state" size="15" />
    <br />
    <br />

    Are you willing to pose nude?
    <input type="radio" name="nude" value="yes" />
    Yes
    <input type="radio" name="nude" value="no" />
    No <br />
    <br />

    Do you have any modeling/acting experience?
    <input type="radio" name="acting" value="yes" />
    Yes
    <input type="radio" name="acting" value="no" />
    No </p>

    <p><br />IMAGES<BR>
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo" type="file" size="30"><br />
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo2" type="file" size="30"><br />
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo3" type="file" size="30"><br />
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo4" type="file" size="30"><br />
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo5" type="file" size="30">

    <br />
    Comments/Questions:<br>
    <textarea name="comments" rows="4" cols="60"></textarea>
    <br />
    <br />


    <input type="submit" value="Submit"/>
    (Privacy & Confidentially Assured)
    <input type="reset" name="Reset" value=" Reset ">
    <img src="images/model_form.jpg" width="316" height="211" align="right"></p>
    </form> </TD>

    HERE IS THE PHP
    <title>insert</title>
    <?php
    $con = mysql_connect("localhost","USERNAME","PASSWORD");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db('MYDB');



    $sql="INSERT INTO Persons (FirstName, email, Age, state, nude, acting, comments, photo, photo2, photo3, photo4, photo5)
    VALUES
    ('$_POST[firstname]', '$_POST[email]', '$_POST[age]', '$_POST[state]', '$_POST[photo]' , '$_POST[nude]' , '$_POST[acting]' , '$_POST[comments]', '$_FILES[photo]' ,'$_FILES[photo2]' , '$_FILES[photo3]' ,'$_FILES[photo4]' ,'$_FILES[photo5]')";

    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    ?>
    <script language="JavaScript" type="text/JavaScript">
    <!--
    window.location.href = "http://www.cash4fetishmodels.com/formsubmitted.htm";
    //-->
    </script>
    <?php

    $first_name=$_POST['firstname'];
    $last_name=$_POST['lastname'];
    $email=$_POST['email'];
    $Age=$_POST['Age'];
    $state=$_POST['state'];
    $photo=$_POST['photo'];
    $nude=$_POST['nude'];
    $acting=$_POST['acting'];
    $comments=$_POST['comments'];

    $photo=($_FILES['photo']['name']);
    $photo2=($_FILES['photo2']['name']);
    $photo3=($_FILES['photo3']['name']);
    $photo4=($_FILES['photo4']['name']);
    $photo5=($_FILES['photo5']['name']);

    //This is the directory where images will be saved
    $target = "images/uploaded/";
    $targetx = $target . basename( $_FILES['photo']['name']);

    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $targetx))
    {

    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }

    $targetx = $target . basename( $_FILES['photo2']['name']);

    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo2']['tmp_name'], $targetx))
    {

    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }

    $targetx = $target . basename( $_FILES['photo3']['name']);

    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo3']['tmp_name'], $targetx))
    {

    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }

    $targetx = $target . basename( $_FILES['photo4']['name']);

    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo4']['tmp_name'], $targetx))
    {

    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }

    $targetx = $target . basename( $_FILES['photo5']['name']);

    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo5']['tmp_name'], $targetx))
    {

    //Tells you if its all ok
    }
    else {
    //Gives and error if its not
    }


    $to = "modelinquiry@cash4fetishmodels.com";
    $from_header = "From: modelinquiry@cash4fetishmodels.com";
    $subject = "Model Request";
    $contents = <<< emailbody
    Dear Webmaster,

    This person has requested more information:
    First Name=$first_name
    Email=$email
    Age=$Age
    State=$state
    Photo=$photo
    Nude=$nude
    Acting=$acting
    Comments=$comments



    emailbody;
    mail($to, $subject, $contents, $from_header);
    mysql_close($con);
    //echo header ("Location: http://cash4fetishmodels.com/formsubmitted.htm");

    ?>

  • #7
    New Coder
    Join Date
    Mar 2009
    Location
    Michigan
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    To the above - please use [code] tags next time to make posting a bit easier to read. Secondly, don't hijack a thread (and especially one that you pretty much copied the code from), make your own topic.

    Anyway, this is your problem:
    PHP Code:
    $sql="INSERT INTO Persons (FirstName, email, Age, state, nude, acting, comments, photo, photo2, photo3, photo4, photo5)
    VALUES
    ('$_POST[firstname]', '$_POST[email]', '$_POST[age]', '$_POST[state]', '$_POST[photo]' , '$_POST[nude]' , '$_POST[acting]' , '$_POST[comments]', '$_FILES[photo]' ,'$_FILES[photo2]' , '$_FILES[photo3]' ,'$_FILES[photo4]' ,'$_FILES[photo5]')"

    You're missing a " '$_POST[nude]' " in the VALUES section after the " '$_POST[state]' ". Also, you should never do anything with $_POST until you are absolutely positive the value is set!

  • #8
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    also please don't post an entire script when it isn't relevant. post only the part of the code where you are getting error problems (i.e. the insert statement).

    you'll also want to look at DATABASE NORMALIZATION, spend your time fixing your data and table structures now before it's got to the point where all your code is working so you'll end up muddling through with crappy data structure.

  • #9
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you!

    I wanted to thank all you guy in this thread. You have been a big help. I still have a few questions but have been asked to start my own topic so will do so. I appreciate the kind advice on protocol and patience with a newbie!!

  • #10
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Quote Originally Posted by inebula88 View Post
    ... but have been asked to start my own topic so will do so.
    I am sure the rules explain it better but, basically, you should start a thread for each specific thing you need help with. no point in asking for help with php and html in the same thread since they need to be in different forums.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #11
    New Coder
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I also found this topic to be helpful. I made an upload file that has four images. I would like to allow users to "update" one, some, or all of the photos at any time using a MySql UPDATE statement.

    When I did this with just one image, I had the variable set up like this:
    Code:
    $encoded = "";    //sets $encoded to blank
    $ownerpic = $_FILES['picture']['name'];
    if (!empty($ownerpic)) {
        copy($picture, "./ownerpics/".$ownerpic."");       
        $encoded = "ownerpics/".$ownerpic;
    }
    and the sql statement started this way:

    Code:
    sql="UPDATE profile SET ";
    if (!empty($encoded)) {
        $sql .= "ownerpic = '$encoded', ";
    }
    $sql .= "id2 = '$id2', etc, etc
    Based on how the multiple upload works, I'm guessing that I can't just set all four pics to blank in the same way, because sometimes users will want to change more than one picture at a time. Any thoughts would be greatly appreciated.

  • #12
    New Coder
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Never mind, I figured it out!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •