Duplication Advice : Records into MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Duplication Advice : Records into MySQL

Post by facets »

Hi Gang,

How would I go about checking to see if an exact 'product name' exists in my DB before writing the row?
Is that the DISTINCT SQL function?

TIA Will.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

SELECT DISTINCT column FROM table
:)
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

Sorry for the waste of space!
It's a MySQL alter table function

ALTER TABLE `tableName` ADD UNIQUE (
`columnName`
)

Now, how do I catch the error nicely :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

To catch Mysql errors in PHP, use the mysql_error() function :)

Code: Select all

<?php

mysql_query("ALTER TABLE tableName ADD UNIQUE (columnName)") or die(mysql_error());

?>
:)

That's of course the most simplistic and not the most secure method of error handling, but gives you the idea and is ideal for development :)
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I'll re-word my question.
How would I catch the error that comes back to browser that sights :

Duplicate entry 'Wills Test' for key 2

Perhaps a javascript alert with the page going back to edit the Name ?
Any idea on how I could do that?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

facets wrote:I'll re-word my question.
How would I catch the error that comes back to browser that sights :

Duplicate entry 'Wills Test' for key 2

Perhaps a javascript alert with the page going back to edit the Name ?
Any idea on how I could do that?
The snippet I posted above will display the error :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$stmt = "insert into tablename values('" . mysql_real_escape_string($_POST['something']) . "')";
if(!mysql_query($stmt)) {
   if( preg_match("/duplicate entry/i", mysql_error()) ) {
        // there was attempt to insert duplicate record, do something (redirect, etc)
   }
} else {
    // entry inserted successfully
}
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I think we may be crossing paths.
I've altered the MySQL column parameters ok (ALTER - ADD UNIQUE etc)
Now i'm adding data into that table/column with the INSERT below and the error displays on the browser :

Duplicate entry 'Wills Test' for key 2

So how would I add a link to 'back' or use an js alert and once OK is clicked go back to the form.

Code: Select all

$sql_add = "INSERT INTO ausapapersummary values ('','$paperCategoryId','$colloPaperName','$manufacturerName','$cpl','$stockId','$adhesiveId','$linerId','$supplierId','$availability', '$features', '$limitations','$productExamples','$suitabilityFoil','$suitabilityYellowLight','$suitabilityLabel','$suitabilityOpacity','$suitabilityBronze','$suitabilityScreen','$suitabilityIceBucket',now(),'')";

from this :
mysql_query($sql_add) or die(mysql_error()); 

to this :

mysql_query("ALTER TABLE ausapapersummary ADD UNIQUE (colloPaperName)") or die(mysql_error());
thanks for the assitance Jenk :)
Post Reply