mynameisG has asked for the wisdom of the Perl Monks concerning the following question:

Hi, so I'm trying to complete an assignment. This assignment includes, creating an HTML form that asks for a table name(table) and the number of columns(field) you want in the table.

Then the first CGI script generates a table where you enter the column name($fieldname), data type ($fieldtype), and field length ($fieldlength). Then based off of the those variables create a statement in a new CGI file that prints basically like this... CREATE TABLE $table ($fieldname, $fieldtype, ($fieldlength))

I figured out how to print all of the information if there is only one field.. i.e. CREATE TABLE example (fname char (10)) The only problem is I can't figure out how to add multiple columns if there is more than one row.. i.e. CREATE TABLE test (id int,fname char (10)) I've tried doing a bunch of research online, but I'm still struggling, I feel like I have to create some kind of loop? But I'm not exactly sure how to do the syntax

here is my code for the first cgi file (createtable.cgi) This file creates a table based on the number of fields entered into the html form

#!/usr/bin/perl use strict; use CGI qw(:standard); print "Content-type: text/html\n\n"; my $table=param('table'); my $field=param('field'); my $fieldname=param('fieldname'); my $fieldtype=param('fieldtype'); my $fieldlength=param('fieldlength'); my $f; #take number of fields and loop around creating a form my $x=<<BEGINX; <form method="input" action="createtable2.cgi" name="form2"> <input type="hidden" name="table" value="$table"> <h1>Table $table</h1> <table border="1"> <tr> <th>Field Name</th> <th>Field Type</th> <th>Field Length</th> </tr> BEGINX for ($f=1;$f<=$field;$f++) { my $fieldname="fieldname" .$f; my $fieldtype="fieldtype" .$f; my $fieldlength="fieldlength" .$f; my $xxx=<<FOO; <tr><td><input type="text" name="fieldname" /> </td> <td><select name="fieldtype"> <option value="integer">int</option> <option value="char">char</option> <option value="float">float</option> <option value="varchar">varchar</option> <option value="date">date</option> <option value="text">text</option> <option value="bool">bool</option> </select></td> <td><input type="text" name="fieldlength" maxlength="4" /> +</tr> FOO $x.= $xxx; } $x.=<<FOO2; </table> <p><b> <input type="submit" name="submit2" value="Create Table" / +> </b></p> </form> FOO2 print $x;

And here is the second cgi file (createtable2.cgi) which connects to the database and is supposed to print out CREATE TABLE $table ($fieldname, $fieldtype, ($fieldlength))

#!/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'); #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;

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

    You're building your HTML table with rows like:

    <tr><td><input type="text" name="fieldname" />

    The name value should probably have a '$' in front:

    <tr><td><input type="text" name="$fieldname" />

    Then, in your second script, read the params in a loop in much the same way as you wrote the names in the first script.

    -- Ken

      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))'

        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

Re: Using perl to Create a Table in a Database
by jethro (Monsignor) on Nov 04, 2010 at 14:19 UTC

    Two hints:

    1) You need to access those constructed variables from perl. You would either have to use the eval function or this:

    for ($f... ... no strict 'refs'; my $x= ${$fieldname.$f}; use strict 'refs';

    Those are called Symbolic References

    2) You need to construct your query string. Remember that '.' is the operator for string concatenation.

    UPDATE: kcott is right, hint 1 is wrong, false, not consistent with the facts and utterly misleading and I should have known better

      jethro, I may be missing something but that doesn't look correct.

      Just looking at the field names and the first loop iteration, he starts off with a normal assignment: my $fieldname="fieldname" .$f; so $fieldname is now "fieldname1".

      His 1st HTML INPUT becomes: <input type="text" name="fieldname1" /> (after interpolating $fieldname in <input type="text" name="$fieldname" />).

      In the second script, he'd do much the same so my $fieldname=param('fieldname'); would become my $fieldname=param('fieldname' . $f); (in a loop).

      -- Ken