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 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The $_POST array and mysql insert problem

    Hi,
    First, thanks in advance for any help.

    I'm trying to build a form where the form fields might repeat according with the user's selection. Like inserting multiple entries of a calendar. Here's a simple example:

    user input: from 1 to 10 fields at a time // Then the page loads thhe form for (for example) 2 entries of
    <input type="text" name="title[]">
    <input type="text" name="location[]">
    <input type="text" name="date[]">

    Now, what I get from print_r($_POST); is the following:

    Array (
    [title] => Array ( [0] => The first one [1] => This is the second )
    [location] => Array ( [0] => USA [1] => UK )
    [date] => Array ( [0] => 2005/09/02 [1] => 2006/02/15 )
    )

    ##--> Here's my difficulty:
    I want to INSERT into the database but the array's are killing me as I'm unable to put the values correctly

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    a simple example:
    PHP Code:
    <?php
    if (!isset($_POST['submit']))
    {
        echo 
    '<form action="" method="POST">';
        
        for (
    $i 1$i <= 3$i++)
        {
            echo 
    $i '<br>
            title: <input type="text" name="data[' 
    $i '][title]"><br>
            location: <input type="text" name="data[' 
    $i '][location]"><br>
            date: <input type="text" name="data[' 
    $i '][date]"><br><br>
            '
    ;
        }
        
        echo 
    '
        <input type="submit" name="submit" value="submit">
        </form>
        '
    ;
    }
    else
    {
        foreach (
    $_POST['data'] as $value)
        {
            echo 
    "INSERT INTO table SET title = '" $value['title'] . "', location = '" $value['location'] . "', date = '" $value['date'] . "'<br>";
        }
    }
    ?>

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply.

    But isn't there a way of parsing the array I posted?

    PHP Code:
    Array ( 
    [
    title] => Array ( [0] => The first one [1] => This is the second )
    [
    location] => Array ( [0] => USA [1] => UK )
    [
    date] => Array ( [0] => 2005/09/02 [1] => 2006/02/15 )


  • #4
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    Quote Originally Posted by MarioPro
    But isn't there a way of parsing the array I posted?
    yes, but that is not the most logical way of grouping the data given the way that you will ultimately do the queries.


  •  

    Posting Permissions

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