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;

In reply to Using perl to Create a Table in a Database by mynameisG

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.