acctman Posted July 25, 2008 Share Posted July 25, 2008 i'm using this code in php to insert IP info into a table sql_query("INSERT INTO $logstable VALUES('$line[m_id]','$line['m_user']',".time().",'$ip')"); If I was to use UPDATE would it insert into the table even if there is no entry to update? If yes, how do I change that INSERT query to work with an UPDATE Link to comment https://forums.phpfreaks.com/topic/116636-update-andor-insert-an-entry/ Share on other sites More sharing options...
fanfavorite Posted July 25, 2008 Share Posted July 25, 2008 No because UPDATE requires WHERE values. You need to run a check: $q = mysql_query("SELECT * FROM $logstable WHERE ip = '$ip'") if (mysql_num_rows($q) > 0) { mysql_query("UPDATE $logstable VALUES('$line[m_id]','$line['m_user']',".time().",'$ip') WHERE ip = '$ip'"); } else { mysql_query("INSERT INTO $logstable VALUES('$line[m_id]','$line['m_user']',".time().",'$ip')"); } Link to comment https://forums.phpfreaks.com/topic/116636-update-andor-insert-an-entry/#findComment-599712 Share on other sites More sharing options...
acctman Posted July 25, 2008 Author Share Posted July 25, 2008 No because UPDATE requires WHERE values. You need to run a check: $q = mysql_query("SELECT * FROM $logstable WHERE ip = '$ip'") if (mysql_num_rows($q) > 0) { mysql_query("UPDATE $logstable VALUES('$line[m_id]','$line['m_user']',".time().",'$ip') WHERE ip = '$ip'"); } else { mysql_query("INSERT INTO $logstable VALUES('$line[m_id]','$line['m_user']',".time().",'$ip')"); } have you ever used INSERT ON DUPLICATE KEY UPDATE, is that better for performance? Link to comment https://forums.phpfreaks.com/topic/116636-update-andor-insert-an-entry/#findComment-599721 Share on other sites More sharing options...
acctman Posted July 26, 2008 Author Share Posted July 26, 2008 what is the actual DUPLICATE KEY? is that the PRIMARY KEY (field) in the table? /* old insert replacing this one */ sql_query("INSERT INTO $logstable VALUES('$line[m_id]','$line['m_user']',".time().",'$ip')") /* new insert with update */ sql_query("INSERT INTO $logstable VALUES('$line[m_id]','$line['m_user']',".time().",'$ip') ON DUPLICATE KEY UPDATE log_user='$line['m_user']',log_time='".time()."',log_ip='$ip'"); Link to comment https://forums.phpfreaks.com/topic/116636-update-andor-insert-an-entry/#findComment-600549 Share on other sites More sharing options...
acctman Posted July 27, 2008 Author Share Posted July 27, 2008 i'm receiving this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING Link to comment https://forums.phpfreaks.com/topic/116636-update-andor-insert-an-entry/#findComment-600610 Share on other sites More sharing options...
samshel Posted July 27, 2008 Share Posted July 27, 2008 i think fanfavorite is correct you will have to fire a query to check if it exists. if IP was your primary key you could have used REPLACE INTO.. Link to comment https://forums.phpfreaks.com/topic/116636-update-andor-insert-an-entry/#findComment-600696 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.