dennismonsewicz Posted April 29, 2008 Share Posted April 29, 2008 I need a way to insert a value into a DB table if the value does not exist if it does then update. Any help? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 29, 2008 Share Posted April 29, 2008 You can either check for it's existence first with another SQL query, or try doing it with a REPLACE http://dev.mysql.com/doc/refman/4.1/en/replace.html Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted April 29, 2008 Author Share Posted April 29, 2008 can you give me an example? Quote Link to comment Share on other sites More sharing options...
craygo Posted April 29, 2008 Share Posted April 29, 2008 here is a simple check. Lets say we want to check to see if a username exists <?php $user= $_POST['username']; $check = "SELECT `username` FROM `users` WHERE `username` = '$user'"; $result = mysql_query($check) or die(mysql_error()); $found = mysql_num_rows($result); if($found > 0){ // update query below } else { // Insert Query below } ?> Ray Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 29, 2008 Share Posted April 29, 2008 For the REPLACE, I would need to know the table structure. But basically, it's the same as an INSERT, except if there is a UNIQUE or PRIMARY KEY column that matches, it will delete the old one and insert the new one. Quote Link to comment 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.