in reply to Re: Using perl to Create a Table in a Database
in thread Using perl to Create a Table in a Database

Earlier when I first wrote the script I did put '$' in front of name="fieldname" etc... and then the output would change to 'CREATE TABLE example (fieldname1 fieldtype1 (fieldlength1)) instead of... 'CREATE TABLE example (fname char (12))'

  • Comment on Re^2: Using perl to Create a Table in a Database

Replies are listed 'Best First'.
Re^3: Using perl to Create a Table in a Database
by kcott (Archbishop) on Nov 04, 2010 at 15:27 UTC

    From your OP: "...I feel like I have to create some kind of loop...".

    I'm suggesting you put a loop in the the second script in the same way that you have a loop in the first script. That way, the names you use in param() should match the names you've used in the HTML form and the values returned by param() should marry up with the values you entered in the HTML form.

    -- Ken

      So you mean like this...?

      #!/usr/bin/perl use strict; use CGI qw(:standard); use DBI; print "Content-type: text/html\n\n"; my $database="assignments"; my $username="web320"; my $password=""; my $fieldname=param('fieldname'); my $fieldtype=param('fieldtype'); my $fieldlength=param('fieldlength'); my $table=param('table'); my $field=param('field'); my $f; for ($f=1;$f<=$field;$f++) { my $fieldname=param('fieldname' .$f); my $fieldtype=param('fieldtype' .$f); my $fieldlength=param('fieldlength' .$f); } #connect to database my $dsn="DBI:mysql:$database:localhost"; my $dbh=DBI->connect($dsn,$username) or die print "doesn't work"; #if +use password, put ,$password my $query = "CREATE TABLE $table ($fieldname $fieldtype ($fieldlength) +)"; #prepare and execute my $sth=$dbh->prepare($query) or die print "bad query"; #$sth->execute(); print $query;

      the only problem i have left now is that it only prints the tablename ($table) and not $fieldname, $fieldtype, $fieldlength i.e. CREATE TABLE test (())

        Yes - like that - you're almost there :-)

        The variables $fieldname, $fieldtype and $fieldlength are lexically scoped (with my) and do not exist outside of the for loop. This is good - don't change that bit.

        You'll need to build your query as you iterate through the for loop. Start the process before you enter the loop:

        ... my $query = "CREATE TABLE $table"; for ($f=1;$f<=$field;$f++) ...

        Now, on each pass through the loop, add the appropriate syntax for each column after you've gathered the data:

        my $fieldname=param('fieldname' .$f); my $fieldtype=param('fieldtype' .$f); my $fieldlength=param('fieldlength' .$f); $query .= "... }

        When you exit the loop, $query should contain your complete CREATE TABLE statement.

        -- Ken

      So you mean like this...?

      #!/usr/bin/perl use strict; use CGI qw(:standard); use DBI; print "Content-type: text/html\n\n"; my $database="assignments"; my $username="web320"; my $password=""; my $fieldname=param('fieldname'); my $fieldtype=param('fieldtype'); my $fieldlength=param('fieldlength'); my $table=param('table'); my $field=param('field'); my $f; for ($f=1;$f<=$field;$f++) { my $fieldname=param('fieldname' .$f); my $fieldtype=param('fieldtype' .$f); my $fieldlength=param('fieldlength' .$f); } #connect to database my $dsn="DBI:mysql:$database:localhost"; my $dbh=DBI->connect($dsn,$username) or die print "doesn't work"; #if +use password, put ,$password my $query = "CREATE TABLE $table ($fieldname $fieldtype ($fieldlength) +)"; #prepare and execute #my $sth=$dbh->prepare($query) or die print "bad query"; #$sth->execute(); print $query;

      the only problem i have left now is that it only prints the tablename ($table) and not $fieldname, $fieldtype, $fieldlength i.e. CREATE TABLE test (())