php array inserted (element by element) into mysql DB

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart Nessux

    php array inserted (element by element) into mysql DB

    The below code works well, except for the database insert. I want each
    element of the array to be inserted into the database, but only the last
    element of the array is inserted... all the elements will echo... any
    ideas? I'm a PHP newbie... you can probably tell by the code:


    if ($_POST['TOURN_ARRAY'])
    {
    foreach ($_POST['TOURN_ARRAY'] as $V)
    {
    echo "Value: $V<br>\n";
    $result = "INSERT INTO tb_name (recruit_id, tourn_id) VALUES
    ('$id', '$V')";
    }
    if (mysql_query($r esult))
    {
    echo("It's all good.<br>");
    }
    else
    {
    echo("It's all bad: " . mysql_error());
    }
    }

  • Pedro Graca

    #2
    Re: php array inserted (element by element) into mysql DB

    Bart Nessux wrote:[color=blue]
    > The below code works well, except for the database insert. I want each
    > element of the array to be inserted into the database, but only the last
    > element of the array is inserted... all the elements will echo... any
    > ideas? I'm a PHP newbie... you can probably tell by the code:
    >
    >
    > if ($_POST['TOURN_ARRAY'])
    > {[/color]

    for the sake of argument lets say your array has two elements, "one" and
    "two"
    [color=blue]
    > foreach ($_POST['TOURN_ARRAY'] as $V)
    > {
    > echo "Value: $V<br>\n";[/color]

    1st time echoes --- Value: one<br>\n
    2nd time echoes --- Value: two<br>\n
    [color=blue]
    > $result = "INSERT INTO tb_name (recruit_id, tourn_id) VALUES ('$id', '$V')";[/color]

    1st time --- $result = "INSERT ... VALUES('???', 'one')"
    2nd time --- $result = "INSERT ... VALUES('???', 'two')"
    [color=blue]
    > }[/color]

    foreach loop ended.

    What is in $result?


    [color=blue]
    >[/color]
    (snip)




    I'd do it more like this

    $values = array();
    foreach ($_POST['TOURN_ARRAY'] as $V) {
    $values[] = "('$id', '$V')";
    }
    $result = 'INSERT ... VALUES ' . implode(', ', $values)



    For my example of "one", "two" in this code above, at the end of
    the loop, $values will be

    {
    [0] => ('???', 'one')
    [1] => ('???', 'two')
    }

    and the final $result will be
    INSERT ... VALUES ('???', 'one'), ('???', 'two')


    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Bart Nessux

      #3
      Re: php array inserted (element by element) into mysql DB

      Pedro Graca wrote:[color=blue]
      > Bart Nessux wrote:
      >[color=green]
      >>The below code works well, except for the database insert. I want each
      >>element of the array to be inserted into the database, but only the last
      >>element of the array is inserted... all the elements will echo... any
      >>ideas? I'm a PHP newbie... you can probably tell by the code:
      >>
      >>
      >> if ($_POST['TOURN_ARRAY'])
      >> {[/color]
      >
      >
      > for the sake of argument lets say your array has two elements, "one" and
      > "two"
      >
      >[color=green]
      >> foreach ($_POST['TOURN_ARRAY'] as $V)
      >> {
      >> echo "Value: $V<br>\n";[/color]
      >
      >
      > 1st time echoes --- Value: one<br>\n
      > 2nd time echoes --- Value: two<br>\n
      >
      >[color=green]
      >> $result = "INSERT INTO tb_name (recruit_id, tourn_id) VALUES ('$id', '$V')";[/color]
      >
      >
      > 1st time --- $result = "INSERT ... VALUES('???', 'one')"
      > 2nd time --- $result = "INSERT ... VALUES('???', 'two')"
      >
      >[color=green]
      >> }[/color]
      >
      >
      > foreach loop ended.
      >
      > What is in $result?
      >
      >
      >
      >
      > (snip)
      >
      >
      >
      >
      > I'd do it more like this
      >
      > $values = array();
      > foreach ($_POST['TOURN_ARRAY'] as $V) {
      > $values[] = "('$id', '$V')";
      > }
      > $result = 'INSERT ... VALUES ' . implode(', ', $values)
      >
      >
      >
      > For my example of "one", "two" in this code above, at the end of
      > the loop, $values will be
      >
      > {
      > [0] => ('???', 'one')
      > [1] => ('???', 'two')
      > }
      >
      > and the final $result will be
      > INSERT ... VALUES ('???', 'one'), ('???', 'two')
      >
      >[/color]

      That works great! Thanks for showing me how to go about it.

      Comment

      • Jason G

        #4
        Re: php array inserted (element by element) into mysql DB

        Hi Bart,

        Using neater syntax will correct this problem in the future:

        - Where did the $id variable come from?
        - READ THE addslashes() documentation in the manual.
        This leaves you wide open to SQL injection attack
        - Notice that your foreach loop was not around your
        mysql_query() call...
        - Asking if $_POST['TOURN_ARRAY'] evaluates to
        true is rather sloppy. You should check to see if it is
        isset() and if it is an array. I used the short-circuting
        feature of PHP operatiors to do this.


        This is the equiv of what you had.....

        <?
        if ($_POST['TOURN_ARRAY'])
        {
        foreach ($_POST['TOURN_ARRAY'] as $V)
        {
        echo "Value: $V<br>\n";
        $result =
        "INSERT INTO tb_name (recruit_id, tourn_id) ".
        "VALUES ('$id', '$V')";
        }

        if (mysql_query($r esult))
        {
        echo("It's all good.<br>");
        }
        else
        {
        echo("It's all bad: " . mysql_error());
        }
        }

        ?>

        ### This is what you should have: ###

        <?
        $id = 1; //Something

        if (isset($_POST['TOURN_ARRAY']) && is_array($_POST['TOURN_ARRAY']))
        {
        foreach($_POST['TOURN_ARRAY'] as $V)
        {
        echo "Value: $V<br>\n";
        $query =
        "INSERT INTO tb_name (recruit_id, tourn_id) ".
        "VALUES ('".addslashes( $id)."', '".addslashes($ V)."')";

        if (mysql_query($r esult))
        {
        echo("It's all good.<br>");
        }
        else
        {
        echo("It's all bad: " . mysql_error());
        }
        }
        }

        ?>

        Comment

        Working...