Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Update mysql table with button on corresponding row

by Anonymous Monk
on Feb 11, 2013 at 15:14 UTC ( #1018159=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to update a sql table based on which button was pushed from the corresponding row. Here is my table and sql query.
$STH10=$DBH->query(' SELECT * FROM equiplog'); print"<table>"; print"<form action='' method='post'>"; while (@data = $STH10->fetchrow_array()) { print "<tr>"; print "<td>$_</td>" for @data[1,2,3]; print "<td>"; print '<center>'; print button( -name => 'returned', -value => $data[0], -label => 'returned' ); print '<center>'; print "</td>"; print "</tr>"; } print"</table>";
So when I push the returned button i would like a sql statement to update that row in my sql table. I dont really understand CGI that well and could use some help.

Replies are listed 'Best First'.
Re: Update mysql table with button on corresponding row
by blue_cowdawg (Monsignor) on Feb 11, 2013 at 16:21 UTC
        I dont really understand CGI that well and could use some help.

    Not too sure how we are supposed to help you. You seem to have some primitive knowledge of how to make Perl scripts talk to a database, but other than the button I see no input to be processed.

    Let me elaborate a bit on a response to this you got earlier: Take a look at CGI, take a step back, and work on some of the examples given in the documentation to learn the CGI half of this.

    Once you understand thoroughly who to create a form, submit the inputs to a back end CGI script you can then move on to do what is known as CRUD which is short hand for Create Retrieve, Update and Delete operations.

    Most databases that I've ever worked on have unique ids for the rows contained within a table. You can use that very much to your advantage. Take a look at this code sniglet:

    use CGI qw/:all/; | much handwaving | foreach my $row ($dbh->fetchrow_hashref){ print start_form ( -action => "" ); print hidden('row_id',$row->{row_id}); printf "Old Name: %s",$row->{name}; printf "New Name: %s",texfield('newname',''); printf submit('action','delete'); printf submit('action','update'); print "<br>"; }
    That will produce a form with a bunch of rows displayed with the corresponding values. A text input will be present for each row and you can write a script that checks to see what is to be done with the input. For instance:
    #!/usr/bin/perl -w use strict; use feature 'switch'; #only works in newer versions (5.10?) of Perl use CGI qw/ :all /; | | Handwaving here. | my $cgi=CGI->new(); my $action = $cgi->param('action'); given ($action){ when('delete'){ my $row_id = $cgi->param('row_id'); | insert code here to delete a row based on its id } when('update'){ my $newname = $cgi->param('newname'); if ($newname) { # non-null input only | code here to execute update } | }

    You got a lot of work to do to get to that point though in terms of learning. Get started! :-)

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Thank you, this is very helpful!
Re: Update mysql table with button on corresponding row
by igelkott (Priest) on Feb 11, 2013 at 15:19 UTC

    Use the CGI module. See examples therein.

      i get that im suppose to use CGI but as i said i dont really understand how i am to do so. Any advice?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018159]
Approved by igelkott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2017-07-23 11:51 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (346 votes). Check out past polls.