Forum Moderators: coopster

Message Too Old, No Replies

Insert Row if There Isn't One Already

         

itledi

2:18 pm on Jan 5, 2008 (gmt 0)

10+ Year Member



I'm trying to update a row with some new values.

However, there are some cases where the row doesn't exist yet, in which case I need to insert a new row.

Right now my current script updates one row, and inserts a second. Any ideas what I'm doing wrong?

mysql_query("UPDATE votes set score=score+'".$vote."', votes=votes+1 where id='".$id."'");
if(mysqli_affected_rows()==0) mysql_query("INSERT INTO votes (id, score, votes) VALUES ('".$id."', '".$vote."', 1)");

I'm going to set id to unique, but I was hoping to have my code structureally correct too.

jatar_k

2:25 pm on Jan 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try changing

if(mysqli_affected_rows()==0)

to

if(mysql_affected_rows()==0)

not sure if the mysqli functions work with the mysql functions, crossing them up could be the issue