MrBillybob Posted August 1, 2007 Share Posted August 1, 2007 I want column XY be unique so: xy | gg | zz | ee 12 | df | df | fd 13 | df | df | fd I want to enter more data but it might enter 12 | df | df | fd again so i would get an error. How would i prevent that without having to loop threw the whole database checking for that entry? Quote Link to comment https://forums.phpfreaks.com/topic/62767-solved-data-and-mysql/ Share on other sites More sharing options...
teng84 Posted August 1, 2007 Share Posted August 1, 2007 use auto increment or query something like if (mysql_query(select * from table where field = somthing)) //the data is already used Quote Link to comment https://forums.phpfreaks.com/topic/62767-solved-data-and-mysql/#findComment-312473 Share on other sites More sharing options...
hitman6003 Posted August 1, 2007 Share Posted August 1, 2007 If there is a second xy equal to 12, do you want it to update that row? Or change xy to be something unique? Quote Link to comment https://forums.phpfreaks.com/topic/62767-solved-data-and-mysql/#findComment-312474 Share on other sites More sharing options...
MrBillybob Posted August 1, 2007 Author Share Posted August 1, 2007 I dont want it to auto increment i just want it to not input it Quote Link to comment https://forums.phpfreaks.com/topic/62767-solved-data-and-mysql/#findComment-312477 Share on other sites More sharing options...
teng84 Posted August 1, 2007 Share Posted August 1, 2007 you have option 1.) query like what i did in my first post 2.) declare the field as unique if u use option 2 you will get sql error if u store same data but you can put some trick to get the error as an asset or as the conditioned material Quote Link to comment https://forums.phpfreaks.com/topic/62767-solved-data-and-mysql/#findComment-312484 Share on other sites More sharing options...
hitman6003 Posted August 1, 2007 Share Posted August 1, 2007 Do the query like normal, but catch the error... $query = "INSERT INTO tablename (col1, col2) VALUES ('a', 'b')"; @mysql_query($query); $error = mysql_errno(); if ($error > 0 && $error == 1022) { echo "Duplicate entry skipped"; } else if ($error > 0 && $error != 1022) { echo mysql_error(); } else { echo "Added!!"; } make sure that column "xy" is a PK or has a unique index. Quote Link to comment https://forums.phpfreaks.com/topic/62767-solved-data-and-mysql/#findComment-312491 Share on other sites More sharing options...
MrBillybob Posted August 1, 2007 Author Share Posted August 1, 2007 Thank you Quote Link to comment https://forums.phpfreaks.com/topic/62767-solved-data-and-mysql/#findComment-312500 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.