Zettai has asked for the wisdom of the Perl Monks concerning the following question:
I have a mysql database that keeps track of user entries.
Not all of the user entries are compulsory and so have default values if the user doesn't enter anything. I am using position markers in my insert statements because I read somewhere that this is safer than just variables. A table for example may look like this:I am then trying to insert into the table with the following:CREATE TABLE foo ( id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, bar VARCHAR(50), blah VARCHAR(50) DEFAULT 'NA', PRIMARY KEY (id) );
If the user didn't enter anything for 'blah', how do I create the sql statement so that it will populate the field with 'NA'? Many thanks, Zettai.sub add_foo { my ($bar, $blah) = @_; my $sth = $dbh->prepare('INSERT INTO foo SET ' . 'bar = ?, ' . 'blah = ?) or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute($bar, $blah) or die "Couldn't execute statement: " . $sth->errstr; my $id = $dbh->last_insert_id(undef, undef, qw(foo id)) or die "no insert id?"; return $id; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using MySQL table's default values upon insert
by graff (Chancellor) on Jan 30, 2010 at 17:14 UTC | |
by Zettai (Acolyte) on Jan 30, 2010 at 20:41 UTC | |
|
Re: Using MySQL table's default values upon insert
by moritz (Cardinal) on Jan 30, 2010 at 08:59 UTC | |
|
Re: Using MySQL table's default values upon insert
by roboticus (Chancellor) on Jan 30, 2010 at 12:28 UTC | |
by Zettai (Acolyte) on Jan 30, 2010 at 20:25 UTC | |
by roboticus (Chancellor) on Jan 30, 2010 at 23:23 UTC | |
|
Re: Using MySQL table's default values upon insert
by stefbv (Priest) on Jan 30, 2010 at 11:33 UTC | |
|
Re: Using MySQL table's default values upon insert
by zwon (Abbot) on Jan 30, 2010 at 10:48 UTC | |
by Zettai (Acolyte) on Jan 30, 2010 at 20:37 UTC |