I'm working on a project where I ask the user to input some data, which gets stored in a database, and then I give the user the chance to input more, change what they've done, or exit out. To do this I pull (some) of what they've entered back out of the database to present it to them.

My test script has uncovered an interesting problem: The first entry works just fine. Data is entered and presented to the user. The second entry however does not get pulled back out of the database and presented to the user. This is despite the fact that the data does get put into the database. (I am using Test::Expect and Test::DatabaseRow to test what is presented to the user and what is stored in the database, respectively.)

Any idea on what is going on?

use strict; use warnings; use DBI; use Term::UI; use Term::Readline; # Offers choices to the user to add or edit a server. sub server { my $term = $_[0]; # Get the database connection, and prepare the query # to get the machine names. my $dbh = db_connect(); my $stmt = $dbh->prepare( q[SELECT name FROM ] . $dbh->quote_identifier('machines') . q[ ORDER BY name;] ); # Read the machine names from the database. my $menu_array = $dbh->selectall_arrayref( $stmt ) or carp("Error reading from the database: $DBI::errstr\n"); $$menu_array[0] = [] unless defined( $$menu_array[0] ); # Now we present the choices to the user, and enter the UI loop. my $answer = $term->get_reply( prompt => 'Select Server: ', choices => [ 'Exit', 'Add Server', @{$$menu_a +rray[0]} ] ); while ( defined($answer) and $answer ne 'Exit' ) { print "$answer\n" if defined($debug); # If they got this far, they either want to add a server, # or they want to manage one. (get_reply() checked for invali +d # responses, and we checked for 'Exit' above.) if ( $answer eq 'Add Server' ) { add_server( $term, $dbh ); } else { manage_server( $term, $dbh, $answer ); } } ## end while ( defined($answer) ...) continue { $menu_array = $dbh->selectall_arrayref( $stmt ) or carp("Error reading from the database: $DBI::errstr\n"); $$menu_array[0] = [] unless defined( $$menu_array[0] ); $answer = $term->get_reply( prompt => 'Select Server: ', choices => [ 'Exit', 'Add Server', @{$$menu_a +rray[0]} ] ); } ## end continue } ## end sub server sub add_server { my ( $term, $dbh ) = @_; # Get the List of OS's from the database. # They then need to be munged into a flat list. my $stmt = $dbh->prepare( q[SELECT name FROM ] . $dbh->quote_identifier('os_types') . q[ ORDER BY name;] ); $stmt->execute() or carp("Error reading from the database: $DBI::e +rrstr\n"); my $os_list = $stmt->fetchall_arrayref( [0] ) or carp("Error reading from the database: $DBI::errstr\n"); $$os_list[0] = [] unless defined( $$os_list[0] ); my @flat_list = map { $$_[0] } @$os_list; # Ok, now we can get all the information from the user... my $server = $term->get_reply( prompt => 'Enter the name of the new server: ' + ); my $ip = $term->get_reply( prompt => 'Enter the IP address of the new ser +ver: ' ); my $os = $term->get_reply( prompt => 'Which OS is on the new serv +er: ', choices => \@flat_list ); if ( !$term->ask_yn( prompt => 'Is this correct?', print_me => "Server Name- $server\nIP Address- $ip\nO +S- $os\n", default => 'y' ) ) { print "Aborting.\n"; return; } ## end if ( !$term->ask_yn( prompt...)) # And once they've given it to us, enter it into the database. my $stmt1 = $dbh->prepare( q[INSERT INTO ] . $dbh->quote_identifier('machines') . q[ ( name, ip, os ) VALUES ( ?, ?, ? ) ] ); my $stmt2 = $dbh->prepare( q[INSERT INTO ] . $dbh->quote_identifier('machine_groups') . q[ ( group_name, machine_name ) VALUES ( ?, ? ) + ] ); if ( $stmt1->execute( $server, $ip, $os ) and $stmt2->execute( $server, $server ) ) { $dbh->commit() or carp("Commit Failed: $DBI::errstr\n"); print "Insert Succeded.\n"; } else { print "Insert Failed: $DBI::errstr\n"; $dbh->rollback(); } return; } ## end sub add_server

In reply to Entering and Retrieving data from a database. by DStaal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.