September 4th, 2000, 04:58 AM
The 'name' column in my table is a UNIQUE column. Therefore the table will not create a new record if the name already exists.
When I try to enter a name which already exists (entering using mysql) it returns an error which says :
Error 2000: Duplicate entry 'plants' for key 4.
Currently, the PHP file does not come up with an error message although the duplicate record has not been entered into the table.
How can I get my PHP file to interpret the mysql error message and alert the user that this name already exists ??
September 4th, 2000, 09:08 AM
I've achieved what I originally wanted. The script searches the table for a match and then displays a message. If a match is not found it inserts the data.
I'd like to know if there's another way where the mysql error on finding a duplicate can be picked up by the script...because in the mthod I've used a lot can happen if many uses are trying add data at the same time.
September 4th, 2000, 09:44 AM
As far as I know, you can't do this directly. You have to do first a query like: "select count(*) from table_name where name='$name'" (which is a pretty fast query); then: $count=mysql_result($result,0,0); if the variable $count is false (e.g. not 0) you can redirect to the file that contains the form using header function with an error message ("This name already exists in our database. Try another one").
September 4th, 2000, 12:08 PM
I think that a better error handling method would be to append a conditional die() function to the end of your query that will print out an error message if the query does not succeed, ex.<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>$result = mysql_query( ... )
or die ("Query failed.");[/code]or you could also check to see if your query handler ($result) == 0 after running the query, and if so print error message and exit()
[This message has been edited by RyanP (edited September 04, 2000).]