SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast rutters's Avatar
    Join Date
    Nov 2002
    Location
    ct
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Perl and DBI Insert???

    Hey there

    I just figured out how to use a select statement to retrieve data from a table and display it on a webpage.

    How would I setup a script that would take information received through and form and input it into a table???

    Jake

  2. #2
    SitePoint Enthusiast eych's Avatar
    Join Date
    Nov 2002
    Location
    Moscow, Russia
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use something like this:

    Code:
    use CGI;
    use DBI;
    
    # creating a new CGI object
    my $cgi = new CGI;
    
    # creating a new DB handler object
    my $dsn = "DBI:mysql:db-name:db-host:db-port";
    my $dbh = DBI->connect($dsn, db-user, db-pass) || die $DBI::errstr;
    
    my $sth = $dbh->prepare('insert into table-name set field1 = ?, field2 = ?, field3 = ?');
    $sth->execute(
      $cgi->param('form-field1'), 
      $cgi->param('form-field2'), 
      $cgi->param('form-field3')
    ) || die $DBI::errstr;
    
    $sth->finish;
    $dbh->disconnect;
    In short, you can use CGI.pm standard Perl module to retrieve form fields and store them in the database. DBI's "execute" method will use the parameters that are given to it and will replace question marks in the "prepare" statement with these parameters. This is the most convinient way to use prepare -> execute pair.
    Last edited by eych; Dec 3, 2002 at 00:06.
    Sergey Smirnov
    One of the Stuffed Guys
    http://www.stuffedguys.com


Bookmarks

Posting Permissions

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