I have made an insert PHP page for inserting a city into my database into the table tblPlace:
tblPlace
placeID
placeName
stateID
How can I prevent that someone inserts e.g. Amsterdam twice in the table ?
Greetz Brain
| SitePoint Sponsor |


I have made an insert PHP page for inserting a city into my database into the table tblPlace:
tblPlace
placeID
placeName
stateID
How can I prevent that someone inserts e.g. Amsterdam twice in the table ?
Greetz Brain


Hmm.. perhaps some clarifications would help us in coming up with a solution.
I'd imagine that placeID is the primary key.. what is StateID? Also, is there a list where users pick these locations or are they simply entering them?
If they're entering them instead of selecting these from a list.. I don't see an easier way to do this other than to query tblPlace and match each placeName with what they entered to see if it's already in it.





You can create a unique index on placeName. That should break when you try and insert dupe values (never tried it in MySQL).
Matt - Sybase DBA / PHP fanatic
Sybase/MySQL/Oracle | I don't like MySQL
Download Sybase | DBForums.com - for all your RDBMS talk


Thanks for the quick reply





i've used the unique field when making a table.
seems to work pretty good. Should shoot a warning back if it exists already.


Unique fields helps and it works. But how can I make my own error page in stead of:
Duplicate entry 'amsterdam' for key 2
Greetz Brain



PHP Code:if (mysql_error()) {
if (strstr(mysql_error(),"Duplicate entry") {
// custom error message/include page here
} else {
// echo normal error message? or do whatever you want
echo mysql_error();
}
}
Kevin


OK, that helps, thank you !
Bookmarks