0

I have a MySQL Insert query, which is likely to produce a duplicate, which results in an error message (with mysql_error()) like this

Duplicate entry '1-79' for key 'PRIMARY'

Is there a way to get this with a little more structure, so I know which key produced the duplicate. Maybe something like an array that looks a little like this

["Error" => "1062", "entry" => "1-79", "key" => "PRIMARY"]

so that i know immediately which column to change.

I do not want an INSERT IGNORE or ON DUPLICATE UPDATE, i just want an easy way to process the error.

Does that exist, or do i have to somehow process the mysql_error() string?

chillichief
  • 1,164
  • 12
  • 22
  • Seems like a bad design if you knowingly would be attempting to insert duplicates and then are going to subsequently change the value of the primary key in an automated fashion afterwards. Also why do you need to parse the message to understand the primary key value that conflicted? You just tried to insert that value. You already have it. – Mike Brant May 19 '16 at 15:23
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly May 19 '16 at 15:26
  • If you must use `mysql_` then look at `mysql_errno()` that will get you an error number, which is much easier to process – RiggsFolly May 19 '16 at 15:27
  • maybe i misphrased that a little bit. i do not want to change it automated, but inform the person in front of the screen, where he messed up – chillichief May 19 '16 at 15:28
  • i have to use `mysql_` because this is an ancient intranet im working on – chillichief May 19 '16 at 15:28
  • Why not just do a search for the submitted info first and if zero results are found THEN do the insert, otherwise show the existing record and maybe offer to update it – Duane Lortie May 19 '16 at 16:30
  • That's how it is done at the moment. I thought, that there could maybe be a clever hack, so i could save some selects – chillichief May 20 '16 at 08:41

0 Answers0