Rifts Posted April 21, 2010 Share Posted April 21, 2010 I'm inserting users into my database using this code // Make a MySQL Connection mysql_connect("mydatabase", "login", "password") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); // Add user to database mysql_query("INSERT INTO members (userid, paid) VALUES ('$user_id', '0')"); the problem is everytime the user load that page it adds him how do I only add if them if there arent added already? thanks Link to comment https://forums.phpfreaks.com/topic/199321-really-simple-question-inserting-data/ Share on other sites More sharing options...
themistral Posted April 21, 2010 Share Posted April 21, 2010 You would need to run a select query first to check if the user already exits in the database. Check if the user_id already exists. Then set up an if statement to say that if the Select query showed no existing member, then run the insert query. Link to comment https://forums.phpfreaks.com/topic/199321-really-simple-question-inserting-data/#findComment-1046108 Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2010 Share Posted April 21, 2010 You can just make the userid column a unique key and let the database enforce uniqueness. Link to comment https://forums.phpfreaks.com/topic/199321-really-simple-question-inserting-data/#findComment-1046109 Share on other sites More sharing options...
Rifts Posted April 21, 2010 Author Share Posted April 21, 2010 could someone toss me an example? Link to comment https://forums.phpfreaks.com/topic/199321-really-simple-question-inserting-data/#findComment-1046118 Share on other sites More sharing options...
AdRock Posted April 21, 2010 Share Posted April 21, 2010 $sql="SELECT userid FROM members WHERE userid='$user_id' LIMIT 1"; $result = mysql_query($sql); $numrows = mysql_num_rows($result); if($numrows == 0 ) { mysql_query("INSERT INTO members (userid, paid) VALUES ('$user_id', '0')"); } else { //already inserted } Link to comment https://forums.phpfreaks.com/topic/199321-really-simple-question-inserting-data/#findComment-1046128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.