Insert into table when user clicks one of many submit buttons

Discussion in 'Perl' started by jwalker, Nov 28, 2011.

  1. jwalker

    jwalker New Member

    Joined:
    Nov 28, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    [FONT=Arial,Helvetica, sans-serif][FONT=Arial,Helvetica, sans-serif]Hi,I am trying to create a site that sells music. I have page with a list of tracks, each with a submit button to buy the track. When a user clicks on one of the submit buttons, the catalog number gets inserted into a table called cart. The problem is that whichever submit button is pressed, only the first row is inserted.
    Each line of the form has a hidden field with a value of the row from the tracks table in the database. Here is the code I have so far:

    foreach ($cgi_object->param())
    {
    $form{$_} = $cgi_object->param($_);
    $catnum = $form{cat_num};
    $row = $form{id};
    }
    my $insert=qq~ insert into cart (id,cat_num) select id,catalog_num from tracks where id ='$row'~;
    my $dbh=DBI->connect($connectionInfo,$user,$passwd);
    my $sth=$dbh->prepare($insert);
    $sth->execute();

    When I execute the mysql statement in the database and replace $id with a row number I get the desired result. I'm guessing the problem has something to do with putting the insert into a loop? Not sure quite how to do this. Any help would be much appreciated. Thanks.
    [/FONT]
    [/FONT]
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    my $insert = qq~ insert into cart (id,cat_num) select id,catalog_num from tracks where id =?~;
    my $dbh    = DBI->connect( $connectionInfo, $user, $passwd );
    my $sth    = $dbh->prepare($insert);
    
    foreach ( $cgi_object->param() ) {
        $form{$_} = $cgi_object->param($_);
        $catnum   = $form{cat_num};
        $row      = $form{id};
    
        $sth->execute($row);
    }
    
     
  3. awatson

    awatson New Member

    Joined:
    Feb 28, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.nexcess.net
    Also, it sounds to me like you might one to go with checkboxes rather than submit buttons so that they can add a bunch of tracks to the cart at once, rather than one at a time...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice