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

my $sth=$dbh->prepare("INSERT INTO images(username,filename) VALUES('$name','$1.jpeg')") ; $sth->execute();
this insert happens on a image file upload. if the image already exists this is the error that I recieve in the browser window
DBD::mysql::st execute failed: Duplicate entry 'HF53120.jpeg' for key +1 at /home/bthcraft/cgi-bin/upload.cgi line 66, line 306
I don't mind the duplicate entries but I would like to display a different message like "Duplicate entries will be overwritten" Thanks John Larson

Replies are listed 'Best First'.
Re: duplicate entry error
by Roger (Parson) on Dec 04, 2003 at 04:36 UTC
    You probably had a primary/unique key set on the filename. If you want to overwrite it, just add a delete SQL before you do the insert. If you want custom error messages, you need to turn off automatic error handling with something like this -
    my $dbh = DBI->connect( blah, blah, {PrintError => 0, RaiseError => 0} ) or die "message"; ... $sth->execute() or die "print your own error message here."
Re: duplicate entry error
by pg (Canon) on Dec 04, 2003 at 04:46 UTC
    "Duplicate entries will be overwritten"

    But this message does not tell the truth.

    Depends on how the table is defined:

    • If you are inserting a duplicate key where it is not allowed, this new row will not be inserted.
    • If you are allowed to insert this row, you really end up with duplicates, but there is no overwriteen. (You can get rid of the duplicates in your application, but that's a different issue. Your error/warning message for your database operation still should reflect the result of that insert statement.)