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 | |
by mynameisG (Novice) on Nov 04, 2010 at 15:03 UTC | |
by kcott (Archbishop) on Nov 04, 2010 at 15:27 UTC | |
by mynameisG (Novice) on Nov 04, 2010 at 16:51 UTC | |
by kcott (Archbishop) on Nov 04, 2010 at 17:57 UTC | |
| |
by mynameisG (Novice) on Nov 04, 2010 at 17:11 UTC | |
|
Re: Using perl to Create a Table in a Database
by jethro (Monsignor) on Nov 04, 2010 at 14:19 UTC | |
by kcott (Archbishop) on Nov 04, 2010 at 14:44 UTC |