widget Posted June 8, 2007 Share Posted June 8, 2007 Hi, I need to insert data into a table but only if it doesnt already exist. I have tried numerous ways including counting rows and nothing works so help desperately needed here. ??? I have a form that submits the information to this page and then redirects back to the form page with the $errorMessage but for the life of me, it keeps on duplicating the data. $query = fetch("SELECT * FROM avatar_blank WHERE `user_name` = '$userid' AND `avatar_name = 'default'"); if($query[avatar_name] == "default"){ $errorMessage = "You+already+have+that+avatar!"; header("Location: avatar.php?&error=$errorMessage"); } else mysql_query("INSERT INTO avatar_blank (user_name , avatar_url, avatar_name) VALUES ('$userid','$avatar_url','$avatar_name')"); $errorMessage = "You+now+have+a+$avatar_name+avatar!"; header("Location: avatar.php?&error=$errorMessage"); ?> Any help is appreciated Here is another example Ive tried without all the values added $query = mysql_query("SELECT * FROM avatar_blank WHERE `user_name` = '$userid' AND `avatar_name = ''"); $num = mysql_numrows($query); if($num < 1){ INSERT INTO `avatar_blank` ( `user_name` , `avatar_url` , `avatar_name` ) VALUES ( '$userid', '', '' ); } Quote Link to comment https://forums.phpfreaks.com/topic/54774-if-not-exist-insert/ Share on other sites More sharing options...
widget Posted June 8, 2007 Author Share Posted June 8, 2007 Seems no1 really knows the answer to this one. I've googled it and none of it makes much sense Quote Link to comment https://forums.phpfreaks.com/topic/54774-if-not-exist-insert/#findComment-270868 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.