Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:17:37+00:00 2026-06-10T16:17:37+00:00

I was wondering if someone would be able to shed some light on how

  • 0

I was wondering if someone would be able to shed some light on how I may overcome this problem.

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 it throws an error, “Please go back and try again”.

I have updated the PHP code with the recommendations given to me so far.

Thank You.

PHP 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';
}
}

Any help will be greatly appreciated.

Finished Code

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:

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:17:38+00:00Added an answer on June 10, 2026 at 4:17 pm

    Your UPDATE syntax is incorrect. You must not repeat the SET keyword:

    $update = "UPDATE ctq_questionnaire SET Item1= '{$questionAns[0]}', Item2 = '{$questionAns[1]}' WHERE user_id = '$memberid'";
    //-------------------------------------------------------------^^^^^^^ no SET here        
    

    For readability it is recommended to enclose the array values in {}, although your way should work.

    Note that your try/catch isn’t going to be of much use since mysql_query() does not throw an exception. Instead it will just return FALSE on error. Instead, store it in a variable and test for TRUE/FALSE as you did with the INSERT.

    // 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.
    }
    

    Finally, and I suspect you’ve heard this before, the old mysql_*() functions are scheduled for deprecation. Consider moving to an API which supports prepared statements, like MySQLi or PDO.

    Update

    Assuming you have a unique index or PK on ctq_questionnaire.user_id on subsequent calls, the first query will error and your second won’t be run. The simplest fix is to use INSERT IGNORE, which will treat key violations as successful.

    $ctqmemberinsert = "INSERT IGNORE INTO ctq_questionnaire (user_id) VALUES ('$memberid')";
    

    A more complicated solution is to first test if the username exists in the table with a SELECT, and if not, do the INSERT.

    $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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if someone would be able to help me out with this
I was wondering if someone would be able to help or point me in
I was wondering if someone would be able to help me write a CQL
I was wondering if someone could provide some insight to as why when I
I'm wondering if someone could look over my code. I'm trying to pass a
I was wondering, how would I be able to compare a variable which is
I was wondering if someone can tell me what would be the best way
Im wondering if someone might be able to help me with something that i
I was wondering if someone might be able to help me solve the Lotka-Volterra
Is this possible? I would like to be able to push a text field's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.