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; }
In reply to Using MySQL table's default values upon insert by Zettai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |