Checking if a row exsists in php?

Cazpa

New member
I need to check and make sure that there are no duplicates of TXN_ID

$sql = "Insert into SALES(ID, AFFID, PRICE, PRODUCT, D, M, Y, H, TXN_ID)
VALUES('','$aff_id','$amount','$item', '$day', '$month', '$year', '$hour', '$pro_id');";
mysql_query($sql) or die(mysql_error());

if there is a duplicate the query needs to do nothing if there is no duplicates the above query needs to be run.

I can't figure the best way to do it without looping through all the entries.

Please provide an example.

Thanks
 
I don't feel like formatting the syntax properly, but doing a SELECT COUNT(*) from SALES where TXN_ID = $PRO_ID could be used to find if the TXN_ID you want to use is already being used. Then you could check to see if the variable you store the COUNT query in is equal to 0 or not.

While that would do what you are asking to do, it is very poor style. In a multi user system (especially one with high volume), it's entirely possible for two (or more) users to run a query on the same TXN_ID at the same time (or close enough to each other to cause problems) that would make them think that they had a free TXN_ID. (i.e. both checks are done before the INSERT for the first transaction takes place) This would result in duplicate TXN_IDs. If the table was defined in such a manner that the TXN_ID was a primary key or had a unique index on it, it would trigger your error handler. If it wasn't defined that way, then you would end up with duplicate TXN_IDs.

Presumably your error handler *COULD* have logic that would try to find another free TXN_ID in it as opposed to displaying an ugly error message. But really all of this over-complicates the problem. You should just use a auto increment feature of MySQL on the TXN_ID column. That guarantees uniqueness and requires no additional logic in your application.
 
I don't feel like formatting the syntax properly, but doing a SELECT COUNT(*) from SALES where TXN_ID = $PRO_ID could be used to find if the TXN_ID you want to use is already being used. Then you could check to see if the variable you store the COUNT query in is equal to 0 or not.

While that would do what you are asking to do, it is very poor style. In a multi user system (especially one with high volume), it's entirely possible for two (or more) users to run a query on the same TXN_ID at the same time (or close enough to each other to cause problems) that would make them think that they had a free TXN_ID. (i.e. both checks are done before the INSERT for the first transaction takes place) This would result in duplicate TXN_IDs. If the table was defined in such a manner that the TXN_ID was a primary key or had a unique index on it, it would trigger your error handler. If it wasn't defined that way, then you would end up with duplicate TXN_IDs.

Presumably your error handler *COULD* have logic that would try to find another free TXN_ID in it as opposed to displaying an ugly error message. But really all of this over-complicates the problem. You should just use a auto increment feature of MySQL on the TXN_ID column. That guarantees uniqueness and requires no additional logic in your application.
 
I don't feel like formatting the syntax properly, but doing a SELECT COUNT(*) from SALES where TXN_ID = $PRO_ID could be used to find if the TXN_ID you want to use is already being used. Then you could check to see if the variable you store the COUNT query in is equal to 0 or not.

While that would do what you are asking to do, it is very poor style. In a multi user system (especially one with high volume), it's entirely possible for two (or more) users to run a query on the same TXN_ID at the same time (or close enough to each other to cause problems) that would make them think that they had a free TXN_ID. (i.e. both checks are done before the INSERT for the first transaction takes place) This would result in duplicate TXN_IDs. If the table was defined in such a manner that the TXN_ID was a primary key or had a unique index on it, it would trigger your error handler. If it wasn't defined that way, then you would end up with duplicate TXN_IDs.

Presumably your error handler *COULD* have logic that would try to find another free TXN_ID in it as opposed to displaying an ugly error message. But really all of this over-complicates the problem. You should just use a auto increment feature of MySQL on the TXN_ID column. That guarantees uniqueness and requires no additional logic in your application.
 
Back
Top