facarroll Posted September 10, 2010 Share Posted September 10, 2010 I have a snippet which only partly works. The code is intended to update a record, but if it does not exist, then create it. This snippet always creates a new record, and will not update an existing one. What's wrong? $query = mysql_query("SELECT equip FROM topics WHERE equip = '$equip' AND managerId = '$userid'"); if (mysql_num_rows($query) > 0) { mysql_query("UPDATE topics SET egroup1 = '$egroup1', egroup2 = '$egroup2', egroup3 = '$egroup3', egroup4 = '$egroup4', egroup5 = '$egroup5', egroup6 = '$egroup6', WHERE equip = '$equip' AND managerId = '$userid'"); } else { mysql_query("INSERT INTO topics (managerId, equip, title, url_big, url_small, egroup1, egroup2, egroup3, egroup4, egroup5, egroup6) VALUES ('$userid','$wordquip', '$equip', '$bigpic', '$smallpic', '$egroup1', '$egroup2', '$egroup3', '$egroup4', '$egroup5', '$egroup6')"); } Link to comment https://forums.phpfreaks.com/topic/213051-creating-a-new-record-and-then-updating-it-whats-wrong-here/ Share on other sites More sharing options...
Adam Posted September 10, 2010 Share Posted September 10, 2010 You have a comma after the last update value: (..)egroup6 = '$egroup6', WHERE equip = '(...) By the way you may want to look into an UPSERT statement here. Link to comment https://forums.phpfreaks.com/topic/213051-creating-a-new-record-and-then-updating-it-whats-wrong-here/#findComment-1109572 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.