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

I have a script that I cannot get a good syntax check on. I have researched and can't figure out while my insert line isn't working. Any ideas.
#!/usr/bin/perl use CGI qw(:standard -debug); print "Content-type: text/html\n\n"; #assign input items to variables my $action = param('action'); my $pname = param('pname'); my $unit = param('unit'); my $qty = param('qty'); my $cost = param('cost'); if ($pname eq "" or $unit eq "" or $qty eq "" or $cost eq "") { print "<html><b><h2><center>Please fill in the form completely. Use yo +ur backspace button to go back and complete the form</h2></b></center +>\n"; print "</HTML>\n"; }else { insert into products (pname, unit, qty, cost) values ('pname', 'unit', + 'qty', 'cost'); }

Replies are listed 'Best First'.
Re: Insert function
by VSarkiss (Monsignor) on Nov 23, 2004 at 02:08 UTC
Re: Insert function
by pg (Canon) on Nov 23, 2004 at 02:35 UTC

    Perl does not support directly inserted SQL statement, as some lauguages do, like Pro*c etc. There might be new concepts to you when you learn DBI. Looking at your SQL statement, I think there is at least one thing you want to pay special attention to: prepared statement and placehold. Eventually, your code should look similar to this:

    use DBI; my $dbh= DBI->connect("dbi:mysql:test", "", "", {RaiseError => 1, Auto +Commit => 0}); my $addprod = $dbh->prepare("insert into products (pname, unit, qty, c +ost) values (?, ?, ?, ?)"); ... $addprod->execute('apple', 'kg', 1, 1.99); ... $dbh->commit();#commit for each unit of work, or rollback, for really +simple cases, you can turn on AutoCommit ... $dbh->disconnect();