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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question select multiple form only sending one value to database

    Hi all. I hope I'm posting this in the right section. I'm having issues uploading select multiple form data into my database. Only the last data value will be sent into the database. The table is setup for INT. for the information. I've also tried varchar.

    Example if I select Comedy set at value="5" , Crime set at value="6", and
    Documentary set at value="7"
    I will only get one value sent to the database. sometimes it's the last value other times the first.

    I've been stuck on this issue for over a day and would love some help.
    Thank you.


    Here is the related code to the issue.

    form

    <p>Genre:</p>
    <p>
    <select multiple name="Genre" id="Genre">
    <option value="0" style="text-indent: 0px;" selected="selected">All</option>
    <option value="1" style="text-indent: 0px;">Action</option>
    <option value="2" style="text-indent: 0px;">Adventure</option>
    <option value="3" style="text-indent: 0px;">Animation</option>
    <option value="4" style="text-indent: 0px;">Biography</option>
    <option value="5" style="text-indent: 0px;">Comedy</option>
    <option value="6" style="text-indent: 0px;">Crime</option>
    <option value="7" style="text-indent: 0px;">Documentary</option>
    <option value="8" style="text-indent: 0px;">Drama</option>
    <option value="9" style="text-indent: 0px;">Family</option>
    <option value="10" style="text-indent: 0px;">Fantasy</option>
    <option value="11" style="text-indent: 0px;">History</option>
    <option value="12" style="text-indent: 0px;">Horror</option>
    <option value="13" style="text-indent: 0px;">Mystery</option>
    <option value="14" style="text-indent: 0px;">Romance</option>
    <option value="15" style="text-indent: 0px;">Sci-Fi</option>
    <option value="16" style="text-indent: 0px;">Short</option>
    <option value="17" style="text-indent: 0px;">Sport</option>
    <option value="18" style="text-indent: 0px;">Thriller</option>
    <option value="19" style="text-indent: 0px;">War</option>
    <option value="20" style="text-indent: 0px;">Western</option>
    </select>

    savetodb

    $Genre = intval(Trim(filter_var($_POST['Genre'], FILTER_SANITIZE_STRING)));


    get the data

    $v_Genre = $result->Genre;
    switch ($v_Genre) {
    case 0: $v_Genre_Desc="All - testing"; break;
    case 1: $v_Genre_Desc="Action"; break;
    case 2: $v_Genre_Desc="Adventure"; break;
    case 4: $v_Genre_Desc="Biography"; break;
    case 5: $v_Genre_Desc="Comedy"; break;
    case 6: $v_Genre_Desc="Crime"; break;
    case 7: $v_Genre_Desc="Documentary"; break;
    case 8: $v_Genre_Desc="Drama"; break;
    case 9: $v_Genre_Desc="Family"; break;
    case 10: $v_Genre_Desc="Fantasy"; break;
    case 11: $v_Genre_Desc="History"; break;
    case 12: $v_Genre_Desc="Horror"; break;
    case 13: $v_Genre_Desc="Mystery"; break;
    case 14: $v_Genre_Desc="Romance"; break;
    case 15: $v_Genre_Desc="Sci-Fi"; break;
    case 16: $v_Genre_Desc="Short"; break;
    case 17: $v_Genre_Desc="Sport"; break;
    case 18: $v_Genre_Desc="Thriller"; break;
    case 19: $v_Genre_Desc="War"; break;
    case 20: $v_Genre_Desc="Western"; break;
    default: $v_genre_Desc="All/Unknown Genre"; break;
    }

    <td>Genre: $v_Genre_Desc </td>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    <select multiple name="Genre[]" id="Genre"
    You'll need to make it an array. PHP will receive it as such, so you'll have a multidimensional array under the $_POST['Genre'], which can be iterated for insertions in a many to one table.

    Also, never ever ever store multiple pieces of data in a single property. If there is a 100% chance that there will never be more or less genres, you can provide a property for each genre at the cost of some dataspace. More useful though would be to use a many to many flattening table so you can add/remove genres as applicable and assign single uhhh, movies maybe, to many genres all without introducing anomalies to the search criteria. So when you want to find a "Short Sci-Fi Mystery WITHOUT romance", that query can be easily done (ie: WHERE genre IN ('short', 'sci-fi', 'mystery') AND genre NOT IN ('romance')[/icode] as a part of a joined nested search).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    bignoob2 (10-24-2013)

  • #3
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for your reply.

    Can you help me out or point me in the right direction to create a multidimensional array under the $_POST['Genre'] I've tried googling on how to do it but I just couldn't figure it out

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,166
    Thanks
    23
    Thanked 601 Times in 600 Posts
    PHP Code:
    <?php
    if(@$_POST['mic'] == 'ro'){
        
    $art = [];
        
    $y 0;
        foreach(
    $_POST["Genre"] as $item){
            
    $art[$y] = $item;
            
    $y++;
        }

        for(
    $y 0$y count($art); $y++){
            echo 
    $art[$y].'<br />';
        }
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    </head>

    <body>

    <p>Genre:</p>
    <form method="POST" action="#" >
    <input type="hidden" name="mic" value="ro" />
    <select name="Genre[]" id="Genre" size="21" multiple>
    <option value="0" style="text-indent: 0px;" selected="selected">All</option>
    <option value="1" style="text-indent: 0px;">Action</option>
    <option value="2" style="text-indent: 0px;">Adventure</option>
    <option value="3" style="text-indent: 0px;">Animation</option>
    <option value="4" style="text-indent: 0px;">Biography</option>
    <option value="5" style="text-indent: 0px;">Comedy</option>
    <option value="6" style="text-indent: 0px;">Crime</option>
    <option value="7" style="text-indent: 0px;">Documentary</option>
    <option value="8" style="text-indent: 0px;">Drama</option>
    <option value="9" style="text-indent: 0px;">Family</option>
    <option value="10" style="text-indent: 0px;">Fantasy</option>
    <option value="11" style="text-indent: 0px;">History</option>
    <option value="12" style="text-indent: 0px;">Horror</option>
    <option value="13" style="text-indent: 0px;">Mystery</option>
    <option value="14" style="text-indent: 0px;">Romance</option>
    <option value="15" style="text-indent: 0px;">Sci-Fi</option>
    <option value="16" style="text-indent: 0px;">Short</option>
    <option value="17" style="text-indent: 0px;">Sport</option>
    <option value="18" style="text-indent: 0px;">Thriller</option>
    <option value="19" style="text-indent: 0px;">War</option>
    <option value="20" style="text-indent: 0px;">Western</option>
    </select>
    <input type="submit">
    </form>
    </body>
    </html>
    <select name="Genre[]" the brackets are needed to make it an array to carry everything selected else it will only carry the last selection.

    In the php section the foreach makes the post into an array.

    The for loop is just to show you what's in the array.

    FYI: use value="Family" instead of value="9" it's easier to understand.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    bignoob2 (10-24-2013)

  • #5
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you!


  •  

    Tags for this Thread

    Posting Permissions

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