Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm new to the posted answer by psu, and will definatly check into that, but from a quick readthrough, you need to be very careful when using those special syntaxes.</p> <p>1 reason that comes to mind: you have no knowledge of what might be happening to the table that you're inserting to or updating info from. If multiple uniques are defined, then you might be in serious trouble, and this is a common thing when scaling applications.</p> <p>2 the replace into syntax is a functionality i rarely wish to happen in my applications. Since i do not want to loose data from colomns in a row that was allready in the table.</p> <p>i'm not saying his answer is wrong, just stating precaution is needed when using it because of above stated reasons and possible more.</p> <p>as stated in the first article, i might be a newbie for doing this but at this very moment i prefer:</p> <pre><code>$result = mysql_query("select user_id from profile where user_id = $user_id limit 1"); if(mysql_num_rows($result) === 1){ //do update like you did } else{ /** * this next line is added after my comment, * you can now also leave the if(count()) part out, since the array will now alwayss * hold data and the query won't get invalid because of an empty array **/ $update_data_profile['user_id'] = $user_id; if(count($update_data_profile)){ $columns = array(); $values = array(); foreach($update_data_profile as $field =&gt; $data){ $columns[] = $field; $values[] = $data; } $sql = "insert into profile (" . implode(",", $columns) .") values ('" . implode("','", $values) . "')" ; var_dump($sql); //remove this line, this was only to show what the code was doing /**update**/ mysql_query($sql) or echo mysql_error(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload