Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COI have changed the function using the above code. The problem is that the table once again can be updated, but no data insert in table when it is empty. I forgot to mention before that I use 2 tables. The 1st table called users and includes the user_id. The other table called profile and includes (profile_id, address, telephone, user_id). In profile table the user_id is the Foreign Key. For the foreign key i did the following set: Internal relation :users.user_id Foreign key constraint `users`.`user_id` ON DELETE RESTRICT ON UPDATE RESTRICT.
      singulars
    2. COwhy would you want to insert a completly empty row into a database? this is the same as no row would be there? I guess i didn't notice that part in your question :p, i'll update the answer, but i really don't get why one would do this
      singulars
    3. COOk I will explain. I have 2 tables in my database. Table USERS holds the user_id and some other info needed during the users' registration. Table PROFILE holds info about users' profile. user_id is a foreign key to profile table. So I made a form that prompts user to create his profile. This form will collect all info about users' profile and will store them in table profile in database. In case that user has not created his profile, profile table will be empty(so...INSERT INTO). If user has already created his profile, he will be able using the same form to update his profile (so...UPDATE).
      singulars
 

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