Results 1 to 7 of 7

Thread: Just only the last item of the data in array goes into the database..

  1. #1
    Join Date
    Feb 2010
    Location
    Toronto
    Posts
    101
    Thanks
    6
    Thanked 1 Time in 1 Post

    Lightbulb Just only the last item of the data in array goes into the database..

    But not whole item of the data.
    I posted the matter at bytes.com. You can find the matter at bytes.com/topic/php/answers/917748-inserting-undefined-number-images-files-its-description-into-mysql-database.
    And I also posted it at http://www.dynamicdrive.com/forums/s...ad.php?t=65229. Anyone can resolve it ?
    Last edited by djr33; 10-14-2011 at 01:36 AM.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,283
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    And yet the link is listed as forbidden.

    Assuming you want the last element of your array use:

    Code:
    <?php
    $fruits = array('apple', 'banana', 'cranberry');
    $end = end($fruits);
    echo "$end";
    ?>
    Last edited by james438; 10-14-2011 at 02:56 AM. Reason: formatting
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Feb 2010
    Location
    Toronto
    Posts
    101
    Thanks
    6
    Thanked 1 Time in 1 Post

    Arrow

    First of all thanks for your initial reply, james438. I wonder why the link didn't work on your computer. It does work on my computer. However the related page URL is http://www.ehyeh.com/validatedformtest3.html. And the following is the form page code.

    Code:
    <html> 
    <head> 
    <title>Submit Form</title> 
    
    <script language="javascript"> 
    <!--  
    fields = 0; 
    images = new Array(); 
    image_title = new Array(); 
      
    function addInput() { 
        if (fields != 3) { 
            var htmlText =  "<input type='file' name='images' id='fields' value='' ACCEPT='filetype/*' />"; 
            var deleteField = "<a style='cursor:pointer;color:yellow;' onclick='this.parentNode.parentNode.removeChild(this.parentNode), fields -=1;'> X</a><br />" 
            var imageTitle = "Title of the photo/image if any<input type='text' name='image_title' id='fields' value='' size='35' /><br /><br />"; 
            var stringConcatenated = htmlText + deleteField + imageTitle; 
            var newElement = document.createElement('div'); 
            newElement.id = ''; // Give the DIV an ID, if you need to reference it later... 
         newElement.innerHTML = stringConcatenated; 
      
            var fieldsArea = document.getElementById('files_list'); 
        fieldsArea.appendChild(newElement); 
      
            fields += 1; 
        } else { 
            alert("Only 3 upload fields allowed."); 
            document.form.add.disabled=true; 
        } 
    } 
    //-->  
    </script> 
    
    </head> 
    <body background="bg1.gif"> 
    <table width="500px" align="center"> 
    <tr><td  bgcolor="#333"> <!-- I tested with '<tr width="500px" align="center">' eliminating table tags, but the result was same as before --> 
    <form action="process-form-data-post.php" method="post" enctype="multipart/form-data" name="wordcount" id="myform" class="cols"> 
    
    <h4>We strictly don't use the input data for other purposes.</h4> 
      
    <fieldset><font=+2> 
    <label> First name * </label><input type="text" name="first_name" id="first_name" required="required" maxlength="25" />  
    <label> Last name * </label><input type="text" name="last_name" id="last_name" required="required" maxlength="25" />   
    </fieldset> 
    <fieldset> 
    <label> E-mail address * </label><input type="email" name="e_list" id="e_list" required="required" maxlength="70" />   
    <label> Living City  </label><input type="text" name="livingcity" id="livingcity" maxlength="25" />  
    </fieldset> 
    <fieldset> 
    <label> Message * &nbsp;&nbsp;Maxlength : 5000 words </label><input type="button" value="Click to Check Words Length" onClick="countit()"> <input type="text" name="wordcount3" size="5"> 
    <textarea rows=15 cols=59 name="message" id="message" wrap="physical" required="required" maxlength="10000" />Under construction.</textarea> <p>  
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /> 
    <input type="button" name="imagesubmit" onclick="addInput()" value="Attaching file/photo(s)" /> 
    <div id="files_list"></div> 
    <script> 
    <!-- Create an instance of the multiSelector class, pass it the output target and the max number of files --> 
    var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 30 ); 
    <!-- Pass in the file element --> 
    multi_selector.addElement( document.getElementById( 'my_file_element' ) ); 
    </script> 
    </font> 
    </fieldset> 
    <fieldset> 
    <label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:OpenWindow('spellcheck2.html','344','285')">SPELL CHECK</a></label> 
    </fieldset> 
    <div class="clear"></div> 
    <button type="submit" name="s1" id="s1">Submit form</button> 
    <button type="button" class="close" onclick="history.back();"> Cancel/Back </button> 
    </form> 
    </td></tr> 
    </table>  
    </body> 
    </html>
    And the following is the processing form data php code.

    PHP Code:
    <?php 
    // Connecting to the MySQL server 
    $db mysql_connect("host""username""password");  
    if (!
    $db
      { 
      die(
    'Could not connect: ' mysql_error()); 
      } 
    mysql_select_db("databasename",$db) or die(mysql_error()); 
      
    // Inserting these values into the MySQL table 
    $_POST[message] = mysql_real_escape_string($_POST[message]); 
    $_POST[first_name] = mysql_real_escape_string($_POST[first_name]); 
    $_POST[last_name] = mysql_real_escape_string($_POST[last_name]); 
    $_POST[livingcity] = mysql_real_escape_string($_POST[livingcity]); 
      
    $uploadDir '/directory/path/'
      
    /* 
    if(count($_POST['images']))  
        {  
            $len = count($_POST['images']);  
      
           for ($i=0; $i < $len; $i++)  
              {   
                  $images = $_POST['images'][$i];  
                  $image_title = $_POST['image_title'][$i];  
              }   
        }  
    */ 
      
    if(isset($_POST['s1'])) 

    if(
    $images) { 
    $fileName $_FILES['images']['name']; 
    $tmpName $_FILES['images']['tmp_name']; 
      
    $filePath $uploadDir $fileName
      
    $result move_uploaded_file($tmpName$filePath); 
    if (!
    $result) { 
    echo 
    "Error uploading file"
    exit; 

      
    if(!
    get_magic_quotes_gpc()) 

    $filePath addslashes($filePath); 
    }  


      
    foreach (
    $filePath as $v1) { 
       
    // foreach ($v1 as $v2) { 
    $query "INSERT INTO databasename (path) VALUES ('$filePath')"
        } 
    //} 
      
    //$filePath = serialize($filePath);   $filePath = implode(',',$filePath); 
    $image_title mysql_real_escape_string($image_title);    //$_POST[image_title] 
    $image_title serialize($image_title);    
    //$image_title = implode(',',$image_title); 'Invalid arguments passed' error message was produced 
    //$image_title = join(" ",$image_title); also 'Invalid arguments passed' error message was produced 
    $query "INSERT INTO databasename (first_name, last_name, e_list, livingcity, message, image_title, submit_date, path) VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[e_list]', '$_POST[livingcity]', '$_POST[message]', '$image_title', NOW(), '$filePath')"
    $result mysql_query($query,$db); 
     if (!
    $result){ 
      die(
    'Error: ' mysql_error()); 
      } 
    echo <<<
    _END 
    <html>  
    <
    head
    <
    title>Processing Form Data</title>  
    <!-- include 
    the Tools --> 
    <
    script src="/jquery.tools.min.js"></script> 
      
    <!-- same styling as in minimal setup -->  
    <link rel="stylesheet" type="text/css" href="/form.css"/> 
      
    <!-- override it to have a columned layout --> 
    <link rel="stylesheet" type="text/css" href="/columns.css"/> 
     
    </head> 
    <body background="bg1.gif" link="white"> 
    <table align="center"> 
    <tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr> 
    </table> 
    <table id="myform" class="cols" align="center"> 
    <tr><td>&nbsp;</td></tr> 
    <tr><td align="center"> 
    <h2>Thank you for submitting your inputs !</h2> 
    </td></tr> 
    <tr><td>&nbsp;</td></tr> 
    <tr><td align="center"><font size="+1"><a href="http://www.ehyeh.com">Back to Home Page</a></font></td></tr><tr><td>&nbsp;</td></tr> 
    </table> 
    </body> 
    </html> 
    _END; 
      
    mysql_close($db); 
    ?>
    And the following is a screenshot of the database table. Only the last item which I input 'a' in the field of 'image_title' was inserted into the database. (I have tested the for loop and foreach loop with 'image_title'.)



    Could it be the problem of the inconsistency of the use of the array in the javascript and the php script ?

    Thanks.
    Last edited by naremkim; 10-14-2011 at 01:34 AM. Reason: adding the name of the poster

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Naremkim, please do not post one question two times. Because this thread is now active, I will delete your other thread.
    Also, if you want help here, then post information here. Don't post a link to an external website and expect us to go there to read it somewhere else.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Feb 2010
    Location
    Toronto
    Posts
    101
    Thanks
    6
    Thanked 1 Time in 1 Post

    Arrow Thanks for your monitoring and managing the threads, djr33.

    I was a little perturbed. (I didn't know that it doesn't like that kind of external link. I just tried to avoid posting long codes. I was greatly helped from DynamicDrive so that I made a link of it on my site.) I posted the thread on this section because I thought this section would be better to get the solution than the orther.
    Well, however, I'd like to pay some money (budget U$30, payment method : Paypal, negotiable), but not much, to the resolver as I posted on the other section.
    Thanks to the readers.
    Last edited by naremkim; 10-14-2011 at 03:50 AM.

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,283
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    I misunderstood. I thought you only wanted the last item of an array.

    I have not tested out your code, but I see that in:

    Code:
    $images = $_POST['images'][$i];
    $images is not set as an array. Use this instead:

    Code:
    $images[]=$_POST['images'];
    I left the [$i] out because it does not look like $_POST is a multidimensional array. Use

    Code:
    print_r($_POST);
    to test this out.

    It looks like if(count($_POST['images'])) will always be true even if $_POST['images']="";
    To choose the lesser of two evils is still to choose evil. My personal site

  7. #7
    Join Date
    Feb 2010
    Location
    Toronto
    Posts
    101
    Thanks
    6
    Thanked 1 Time in 1 Post

    Arrow Thanks for your response, james438.

    To make the issue simple I want to narrow the variables to one, the image_title. The image_title was initialized as global variable in the javascript in html page. The php code uses the variable in the mysql_real_escape_string() and consequently serialize().
    Do I have to insert the line
    Code:
    $image_title[]=$_POST['image_title'];
    into the php code to get the result ? Will it be that all I need ?

    Thanks.

    P.S. I tested it but it didn't work. The error message came out was '[] operator not supported'. What will be the problem ?
    Last edited by naremkim; 10-17-2011 at 10:39 AM. Reason: For adding P.S.

Bookmarks

Posting Permissions

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