Dynamic Form Update

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Keightley

    Dynamic Form Update

    I have the following form in which i need to updateitems.
    The problem is that i do not know how many elements will be in the form
    (generated dynamically)

    eg
    <input type = "hidden" name ="id1" value="1"><inpu t type ="qty1" value ="1">
    <input type = "hidden" name ="id2" value="2"><inpu t type ="qty2" value ="1">
    <input type = "hidden" name ="id3" value="3"><inpu t type ="qty3" value ="5">
    <input type = "hidden" name ="idn" value="n"><inpu t type ="qtyn" value ="1">


    how do i generate an array for a foreach loop to update the values in PHP
    so that i can use it to create a Update query
    eg
    ///begin loop
    $sql = "UPDATE `table` SET `qty` = `$qtyn` WHERE `id` = `$idn`";
    // end loop


    many thanks for any help
    Craig


  • Daniel Tryba

    #2
    Re: Dynamic Form Update

    In comp.lang.php Craig Keightley <dont@spam.me > wrote:[color=blue]
    > <input type = "hidden" name ="idn" value="n"><inpu t type ="qtyn" value ="1">[/color]

    This doens't make any sense in html.
    [color=blue]
    > how do i generate an array for a foreach loop to update the values in PHP[/color]

    <input type='hidden' name='qty[n]' value='x'>

    should give you an array in _GET/_POST with the name qty, keys are 'n'
    and values are 'x'.

    FUP: comp.lang.php

    Comment

    • Craig Keightley

      #3
      Re: Dynamic Form Update

      My Mistake, the HTML should read as:
      <input type = "hidden" name ="idn" value="n"><inpu t type ="text" name="qtyn"
      value ="1">

      "Daniel Tryba" <partmapsswen@i nvalid.tryba.nl > wrote in message
      news:427f27a5$0 $52565$c5fe704e @news6.xs4all.n l...[color=blue]
      > In comp.lang.php Craig Keightley <dont@spam.me > wrote:[color=green]
      >> <input type = "hidden" name ="idn" value="n"><inpu t type ="qtyn" value
      >> ="1">[/color]
      >
      > This doens't make any sense in html.
      >[color=green]
      >> how do i generate an array for a foreach loop to update the values in
      >> PHP[/color]
      >
      > <input type='hidden' name='qty[n]' value='x'>
      >
      > should give you an array in _GET/_POST with the name qty, keys are 'n'
      > and values are 'x'.
      >
      > FUP: comp.lang.php[/color]


      Comment

      • Craig Keightley

        #4
        Re: Dynamic Form Update

        Sorted: Many thanks for the nudge

        if(isset($_POST['update']) && ($_POST['update'] == "true")){
        mysql_select_db ($database_conn , $conn);
        foreach ($qty as $v => $key) {
        $sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
        cookieRef='".$I D."'";
        mysql_query($sq l, $conn) or die(mysql_error ());
        }
        header("Locatio n: basket.php");
        }



        "Daniel Tryba" <partmapsswen@i nvalid.tryba.nl > wrote in message
        news:427f27a5$0 $52565$c5fe704e @news6.xs4all.n l...[color=blue]
        > In comp.lang.php Craig Keightley <dont@spam.me > wrote:[color=green]
        >> <input type = "hidden" name ="idn" value="n"><inpu t type ="qtyn" value
        >> ="1">[/color]
        >
        > This doens't make any sense in html.
        >[color=green]
        >> how do i generate an array for a foreach loop to update the values in
        >> PHP[/color]
        >
        > <input type='hidden' name='qty[n]' value='x'>
        >
        > should give you an array in _GET/_POST with the name qty, keys are 'n'
        > and values are 'x'.
        >
        > FUP: comp.lang.php[/color]


        Comment

        • ScareCrowe

          #5
          Re: Dynamic Form Update


          --ScareCrowe
          "Daniel Tryba" <partmapsswen@i nvalid.tryba.nl > wrote in message
          news:427f27a5$0 $52565$c5fe704e @news6.xs4all.n l...[color=blue]
          > In comp.lang.php Craig Keightley <dont@spam.me > wrote:[color=green]
          > > <input type = "hidden" name ="idn" value="n"><inpu t type ="qtyn" value[/color][/color]
          ="1">[color=blue]
          >
          > This doens't make any sense in html.
          >[color=green]
          > > how do i generate an array for a foreach loop to update the values in[/color][/color]
          PHP[color=blue]
          >
          > <input type='hidden' name='qty[n]' value='x'>
          >
          > should give you an array in _GET/_POST with the name qty, keys are 'n'
          > and values are 'x'.
          >
          > FUP: comp.lang.php[/color]

          Actually, it makes sense to me though. ( <input type="hidden" name="idn"
          value="n"> is correct HTML, although it is spaced incorrectly, it is just as
          good as spelling doesn't like so: doens't ;P )
          I think he is asking how to insert multiple entries into a DB passed from a
          form.
          Assuming you know how many entries there will be, just iterate thru it with
          a for loop. If you have 10 entries, for($a=0;$a<10; $a++) etc etc.

          Otherwise:

          I do agree using form element variables as an array like id[1], id[2]
          instead of id1, id2. This way, when you go through the loop you can do
          something like this:
          $id_count = count($_POST['id']);
          for($b=0;$b<$id _count;$b++){
          mysql_query("IN SERT INTO blahblah values ($_POST[id][$b],
          $_POST[qty][$b]");
          }
          Not CnP, but you should get the point from that!

          --ScareCrowe


          Comment

          • Peter Fox

            #6
            Re: Dynamic Form Update

            Following on from ScareCrowe's message. . .
            [color=blue]
            >I do agree using form element variables as an array like id[1], id[2]
            >instead of id1, id2. This way, when you go through the loop you can do
            >something like this:
            >$id_count = count($_POST['id']);
            >for($b=0;$b<$i d_count;$b++){
            > mysql_query("IN SERT INTO blahblah values ($_POST[id][$b],
            >$_POST[qty][$b]");
            >}[/color]

            This is lots of inserts (or updates) when one would suffice.
            INSERT INTO aTable name,address,te lno VALUES ('Fred Smith', '1 High
            St.','01376 515151')

            So do something like this to economise on DB access
            $vals = implode("','",a rray_values($PO ST));
            $sql = "Insert .....values('$v als')";
            (NB This isn't meant to be real working code - there are lots of issues
            with it - just an illustration.)



            --
            PETER FOX Not the same since the bottom fell out of the bucket business
            peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
            2 Tees Close, Witham, Essex.
            Gravity beer in Essex <http://www.eminent.dem on.co.uk>

            Comment

            • Peter Fox

              #7
              Re: Dynamic Form Update

              Following on from ScareCrowe's message. . .
              [color=blue]
              >I do agree using form element variables as an array like id[1], id[2]
              >instead of id1, id2. This way, when you go through the loop you can do
              >something like this:
              >$id_count = count($_POST['id']);
              >for($b=0;$b<$i d_count;$b++){
              > mysql_query("IN SERT INTO blahblah values ($_POST[id][$b],
              >$_POST[qty][$b]");
              >}[/color]

              This is lots of inserts (or updates) when one would suffice.
              INSERT INTO aTable name,address,te lno VALUES ('Fred Smith', '1 High
              St.','01376 515151')

              So do something like this to economise on DB access
              $vals = implode("','",a rray_values($PO ST));
              $sql = "Insert .....values('$v als')";
              (NB This isn't meant to be real working code - there are lots of issues
              with it - just an illustration.)



              --
              PETER FOX Not the same since the bottom fell out of the bucket business
              peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
              2 Tees Close, Witham, Essex.
              Gravity beer in Essex <http://www.eminent.dem on.co.uk>

              Comment

              • smilesinblues@gmail.com

                #8
                Re: Dynamic Form Update

                Hello everyone,
                I have a somewhat same problem.
                Craig, from your code

                foreach ($qty as $v => $key) {
                $sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
                cookieRef='".$I D."'";

                ==> qty=$key

                $key contains the values of qty passed from last page, with me qty for
                all products is different....li ke.

                qty1 = 2
                qty2 = 7
                qty3 = 4
                and so on

                I have not been able to pass the primary key to update the DB table so
                i use

                foreach ($_POST as $v => $key) {
                $sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
                cookieRef='".$I D."'";

                but even the the primary key is missing and update is not done on all
                products.

                please help.
                Thanks a lot.
                Jaunty Edward

                Comment

                • smilesinblues@gmail.com

                  #9
                  Re: Dynamic Form Update

                  Hello everyone,
                  I have a somewhat same problem.

                  Craig, from your code:

                  foreach ($qty as $v => $key) {
                  $sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
                  cookieRef='".$I D."'";


                  ==> qty=$key

                  $key contains the values of qty passed from last page, unlike you for
                  me qty for all products is different....li ke.

                  qty1 = 2
                  qty2 = 7
                  qty3 = 4
                  and so on

                  I have not been able to pass the primary key to update the DB table so
                  i use

                  foreach ($_POST as $v => $key) {
                  $sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
                  cookieRef='".$I D."'";

                  but even then the primary key is missing and update is not done on all
                  products, somehow product reference is not passed.

                  please help.
                  Thanks a lot.
                  Jaunty Edward

                  Comment

                  • smilesinblues@gmail.com

                    #10
                    Re: Dynamic Form Update

                    Hello everyone,
                    I have a somewhat same problem.

                    Craig, from your code:

                    foreach ($qty as $v => $key) {
                    $sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
                    cookieRef='".$I D."'";


                    ==> qty=$key

                    $key contains the values of qty passed from last page, unlike you for
                    me qty for all products is different....li ke.

                    qty1 = 2
                    qty2 = 7
                    qty3 = 4
                    and so on

                    I have not been able to pass the primary key to update the DB table so
                    i use

                    foreach ($_POST as $v => $key) {
                    $sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
                    cookieRef='".$I D."'";

                    but even then the primary key is missing and update is not done on all
                    products, somehow product reference is not passed.

                    please help.
                    Thanks a lot.
                    Jaunty Edward

                    Comment

                    Working...