9911782 Posted August 3, 2006 Share Posted August 3, 2006 HiIm am doing the web-based application, where I have to add some information from front-end, and then add it to the database. Now, I would like to get the MySQL statement that will prevent me from adding same thing in 1 table.Please help me, I need to do this by end of today.thank you Quote Link to comment https://forums.phpfreaks.com/topic/16417-mysql-help-need-to-prevent-the-duplication-of-information-when-inserting-to-db/ Share on other sites More sharing options...
manmadareddy Posted August 3, 2006 Share Posted August 3, 2006 I hope u need only some fields which are not replicated I mean unique like email.first identify those fields and just write a select query .$sql="select count(*) as cnt from tablename where email!='".$_REQUEST['email']."'";//you can add if you need to check another fields too like $sql." and fname!='".$_REQUEST['fname']."'";$res=mysql_query($sql);$result=mysql_fetch_assoc($res);if(!$result['cnt']){ //here insert statement}else{ //email already exists error message //same form with $_REQUEST fields filled} Quote Link to comment https://forums.phpfreaks.com/topic/16417-mysql-help-need-to-prevent-the-duplication-of-information-when-inserting-to-db/#findComment-68383 Share on other sites More sharing options...
9911782 Posted August 3, 2006 Author Share Posted August 3, 2006 Hi manmadareddyits working right now, thank you very much. I took ur advise.thanks a million. Quote Link to comment https://forums.phpfreaks.com/topic/16417-mysql-help-need-to-prevent-the-duplication-of-information-when-inserting-to-db/#findComment-68492 Share on other sites More sharing options...
fenway Posted August 3, 2006 Share Posted August 3, 2006 I'm sorry to say that it's not very good advice... first, never use !=, because no index will be able to utilized. Second, you don't need to fetch the rows at all... simply use mysql_num_rows() and see if you get back anything. Third, depending on the application, a UNIQUE KEY restriction on the column is often the better way to go. Quote Link to comment https://forums.phpfreaks.com/topic/16417-mysql-help-need-to-prevent-the-duplication-of-information-when-inserting-to-db/#findComment-68561 Share on other sites More sharing options...
manmadareddy Posted August 4, 2006 Share Posted August 4, 2006 Yeah! you are right..I am very sorry for my bad suggestion.You have given good suggestions for me.I am very much thankful to you. Quote Link to comment https://forums.phpfreaks.com/topic/16417-mysql-help-need-to-prevent-the-duplication-of-information-when-inserting-to-db/#findComment-68990 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.