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? Link to comment https://forums.phpfreaks.com/topic/103467-solved-if-no-update-do-insert/ 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 Link to comment https://forums.phpfreaks.com/topic/103467-solved-if-no-update-do-insert/#findComment-529825 Share on other sites More sharing options...
dennismonsewicz Posted April 29, 2008 Author Share Posted April 29, 2008 can you give me an example? Link to comment https://forums.phpfreaks.com/topic/103467-solved-if-no-update-do-insert/#findComment-529832 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 Link to comment https://forums.phpfreaks.com/topic/103467-solved-if-no-update-do-insert/#findComment-529839 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. Link to comment https://forums.phpfreaks.com/topic/103467-solved-if-no-update-do-insert/#findComment-529846 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.