in reply to (jeffa) Re: dbi insert
in thread dbi insert

I have tried most of what has been suggested, but I still cannot get it to work.
It sends back NO error code, but it does not insert any data in my db

******************code*********************

#!/usr/local/bin/perl -wT

use strict;
use CGI;
use DBI;

my $cgi = new CGI;
my $fornavn = $cgi->param("fornavn");
my $efternavn = $cgi->param("efternavn");
my $cpr = $cgi->param("cpr");
my $adresse = $cgi->param("adresse");
my $zip = $cgi->param("zip");
my $city = $cgi->param("city");
my $tjenestested = $cgi->param("tjenestested");


my $dbh = DBI->connect("DBI:mysql:um","root","") ||
die "Could not connect: $DBI::errstr\n";

my $sth = $dbh->prepare("INSERT into people (fornavn,
efternavn, cpr, adresse, zip, city, tjenestested)
values ($fornavn, $efternavn, $cpr, $adresse, $zip,
$city, $tjenestested)");

$sth->execute();
$sth->finish();


$dbh->disconnect;

print "Content-type: text/html\n\n";
print "<HTML>";
print "<HEAD>";
print '<META HTTP-EQUIV="Refresh" CONTENT="1; URL=um.htm">';
print "</HEAD>";
print '<BODY bgcolor="#333366">';
print "</BODY>";
print "</HTML>";

******************code*********************

Replies are listed 'Best First'.
Re: Re: (jeffa) Re: dbi insert
by tommyw (Hermit) on Oct 17, 2001 at 12:45 UTC
    Try changing the connect call to:
    my $dbh=DBI->connect('DBI:mysql:um', 'root', '', {AutoCommit=>1, RaiseError=>1});
    The RaiseError value means that any subsequent DBI error will cause your program to die with an error message. AutoCommit explicitly controls whether you require a commit call. mysql used not to support transactions, so committing was irrelevant. I don't know if this is still true.

        Ok, now I'm really worried. That error message is telling you quite clearly that you're attempting to access the firstname field, from the field list table. And you can't, because there's no such field in that table

        I'm worried because there are no appropriate references to such a field, table or even a variable called $firstname in your code

        Oh, hang on:

        my $sth = $dbh->prepare("INSERT into people (fornavn, efternavn, cpr, adresse, zip, city, tjenestested) values ($fornavn, $efternavn, $cpr, $adresse, $zip, $city, $tjenestested)");
        will result in the SQL statement being something along the lines of: INSERT into people(fornavn...) values (tommy, ...);. Which is, of course, incorrect: those value strings need to be quoted. You see all those earlier posts with many, many question marks in them? Well, this is exactly why they were there. Try changing your statement in a similar fashion

        Yeah, that's not good. If tommyw's suggestion doesn't fix the problem (which i think it will), then i recommend logging directly into the mysql database server and issue the following commands:
        1. show databases - this will show you a list of the available databases, make sure 'um' is among, them - if not, then find out which database you need to use
        2. use um - assuming you found it in #1
        3. desc people; - (notice the semi-colon) this will give you a tabular text table containing the field names, their types, and more stuff about the fields.
        If you do not see 'firstname' among them - well, that's beyond the scope of this site. But i can point you to the mysql docs, in particular, the alter table syntax.

        Pertaining to your 3 errors that you listed (which should have been wrapped in <code> tags i might add), the first and third errors are most likely just side effects from the second error. Fix the second and the other two should go away as well. :)

        good luck!
        jeffa