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 8106127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:22:16+00:00 2026-06-06T00:22:16+00:00

I have an issue where instead of checking to see if there’s a duplicate,

  • 0

I have an issue where instead of checking to see if there’s a duplicate, the script below just inserts the row into mysql, no updating at all. I’ve tried again and again yet there’s always duplication going on.

What exactly am I doing wrong here?

if ($completeStatus == "0") {

// Get the default questions responses and insert into database
$questionsAnswered = $_GET['questions'];
foreach( $questionsAnswered as $key => $answers){
    $query = "INSERT INTO survey_ResponseDetail (responseHeader,
                        questionID,
                        questionText,
                        ansLikelihood,
                        ansExpConsq,
                        ansRepRisk,
                        currWayMitigate
                    )
VALUES ('$ResponseHeader',
        '$key',
        'test1',
        '$answers[1]',
        '$answers[2]',
        '$answers[3]',
        '$answers[4]'
        )
ON DUPLICATE KEY UPDATE ansLikelihood = '$answers[1]',
                        ansExpConsq = '$answers[2]',
                        ansRepRisk = '$answers[3]',
                        currWayMitigate = '$answers[4]',
                        id = LAST_INSERT_ID(id)";
    $dbresult = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());

}
  • 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-06T00:22:18+00:00Added an answer on June 6, 2026 at 12:22 am

    Assuming your assertion in the comments is correct, the problem is that you haven’t defined a unique constraint or primary key on any fields in survey_ResponseDetail.

    Per the MySQL Documentaiton on the ON DUPLICATE KEY UPDATE syntax

    If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that
    would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an
    UPDATE of the old row is performed.

    If you don’t designate a field as unique (or indirectly doing so by marking it as a PK) then MySQL has no way of knowing what a duplicate key is. Thus the command isn’t doing what you expect.

    If you tell me which field (or combination of fields) must be unique for each row in survey_ResponseDetail I will edit my answer with the code to apply the constraint you need to make this work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to solve an issue for someone else and instead have run into my
I have a classifieds website where users may sell/buy just about anything... My issue
i have an issue here, my mails sent from my PHP script never gets
I have issue with: <form:checkboxes path=roles cssClass=checkbox items=${roleSelections} /> If previous line is used
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
We are new to ROR, We have issue in creating Login/Logout process in ROR
Share your ideas please! I have issue to check the folder and convert a
Have an issue with marshall and unmarshall readers and writers. So here it is.
I have an issue with jquery and history.back(): I got a link: <a href=#
I have an issue of alphabetically sorting the contacts picked from the address book

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.