Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your <code>UPDATE</code> syntax is incorrect. You must not repeat the <code>SET</code> keyword:</p> <pre><code>$update = "UPDATE ctq_questionnaire SET Item1= '{$questionAns[0]}', Item2 = '{$questionAns[1]}' WHERE user_id = '$memberid'"; //-------------------------------------------------------------^^^^^^^ no SET here </code></pre> <p>For readability it is recommended to enclose the array values in <code>{}</code>, although your way should work. </p> <p>Note that your <code>try/catch</code> isn't going to be of much use since <code>mysql_query()</code> does not throw an exception. Instead it will just return <code>FALSE</code> on error. Instead, store it in a variable and test for <code>TRUE/FALSE</code> as you did with the <code>INSERT</code>.</p> <pre><code>// We assume these values have already been validated and escaped with mysql_real_escape_string()... $update = "UPDATE ctq_questionnaire SET Item1= '{$questionAns[0]}', Item2 = '{$questionAns[1]}' WHERE user_id = '$memberid'"; $upd_result = mysql_query($update); if ($upd_result) { // ok } else { // error. } </code></pre> <p>Finally, and I suspect you've heard this before, the old <code>mysql_*()</code> functions are scheduled for deprecation. Consider moving to an API which supports prepared statements, like MySQLi or PDO.</p> <h3>Update</h3> <p>Assuming you have a unique index or PK on <code>ctq_questionnaire.user_id</code> on subsequent calls, the first query will error and your second won't be run. The simplest fix is to use <code>INSERT IGNORE</code>, which will treat key violations as successful.</p> <pre><code>$ctqmemberinsert = "INSERT IGNORE INTO ctq_questionnaire (user_id) VALUES ('$memberid')"; </code></pre> <p>A more complicated solution is to first test if the username exists in the table with a <code>SELECT</code>, and if not, do the <code>INSERT</code>.</p> <pre><code>$exists_q = mysql_query("SELECT 1 FROM ct1_questionnaire WHERE user_id = '$memberid'"); if (mysql_num_rows($exists_q) === 0) { // Doesn't exist. Do the INSERT query } // proceed to the UDPATE after INSERTing if necessary </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.
 

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