Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Insert and Update in a single function
    primarykey
    data
    text
    <p>I was wondering if someone would be able to shed some light on how I may overcome this problem. </p> <p>I'm trying to add and update information on a database, so when a user first enters completes the questionnaire its fine and it works, However when they go back to update the questionnaire <strong>it throws an error</strong>, "Please go back and try again". </p> <p>I have updated the PHP code with the recommendations given to me so far.</p> <p>Thank You. </p> <p>PHP code: </p> <pre><code>function updatePartCTQ_part1($questionAns, $memberid) { //First Insert MemberID $ctqmemberinsert = "INSERT INTO ctq_questionnaire (user_id) VALUES ('$memberid')"; $addresult = mysqli_query($ctqmemberinsert); if ($addresult) { $update = "UPDATE ctq_questionnaire SET Item1= '{$questionAns[0]}', Item2 = '{$questionAns[1]}' WHERE user_id = '$memberid'"; mysqli_query($conn, $update); } else { echo 'Please go back and try again'; } } </code></pre> <p>Any help will be greatly appreciated.</p> <p><strong>Finished Code</strong></p> <p>Thanks to Michael and the rest of the guys I was able to get the code working, so I thought I'd post an update, if anyone else gets stuck they'd be able to have a glance at the working version of the code: </p> <pre><code>function updatePartCTQ_part1($questionAns, $memberid) { //Check whether user exists $exists = mysql_query("SELECT * FROM ct1_questionnaire WHERE user_id = '$memberid'"); if (mysql_num_rows($exists) === 0) { // Doesn't exist. INSERT User into Table $ctqmemberinsert = "INSERT INTO ctq_questionnaire (user_id) VALUES ('$memberid')"; mysqli_query($ctqmemberinsert); } // UDPATE after INSERT $update = "UPDATE ctq_questionnaire SET Item1= '{$questionAns[0]}', Item2 = '{$questionAns[1]}, Item3 = '{$questionAns[2]}', Item4 = '{$questionAns[3]}',Item5 = '{$questionAns[4]}', Item6 = '{$questionAns[5]}', Item7 = '{$questionAns[6]}', Item8 = '{$questionAns[7]}', Item9 = '{$questionAns[8]}', Item10 = '{$questionAns[9]}', Item11 = '{$questionAns[10]}', Item12 = '{$questionAns[11]}', Item13 = '{$questionAns[12]}', Item14 = '{$questionAns[13]}', Item15 = '{$questionAns[14]}' WHERE user_id = '$memberid'"; mysql_query($update); } </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.
 

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