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

  • Home
  • SEARCH
  • 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 7420715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:17:09+00:00 2026-05-29T08:17:09+00:00

I was trying to upload a csv file to update a table in my

  • 0

I was trying to upload a csv file to update a table in my db, but also wanted to prevent duplication. Initially a single DUPLICATE worked but when i added the second condition it didn’t work.
I got “Duplicate entry ‘1’ for key PRIMARY”. How can i achieve my aim.

thanks for your help.

$sql = "INSERT INTO biodata (student_number, fname, lname, course_name, level)
        VALUES('$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]')
        DUPLICATE KEY UPDATE student_number = student_number
        AND course_name = course_name";

 mysql_query($sql) or die (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-05-29T08:17:10+00:00Added an answer on May 29, 2026 at 8:17 am

    Replace AND with comma , in the UPDATE part.

    Use the VALUES() syntax to refer to the values passed.

    Also, you don’t need to update the Primary or Unique Key (that activated the ON DUPLICATE KEY UPDATE):

    $sql = "
      INSERT into biodata
        (student_number, fname, lname, course_name, level) 
      VALUES
        ('$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]')
      ON DUPLICATE KEY UPDATE 
                          --- student_number = VALUES(student_number),
                          --- removed
          fname = VALUES(fname),
          lname = VALUES(lname),
          course_name = VALUES(course_name),
          level = VALUES(level)
    " ;
    

    What the above code does:

    If the INSERT fails (there is already a student with this student_number you are trying to Insert), the UPDATE is activated and the 4 other fields are updated.

    It’s your choice which fields to update. If you want to update for example, only the course_name field, just keep that line and remove the rest.

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

Sidebar

Related Questions

I'm trying to upload this CSV file onto my table in pgsql, but I'm
I'm trying to upload a 95 GB CSV file into a MySQL database (MySQL
I am trying to upload a CSV file to Oracle database from my client
I am trying to upload a moderetly sized csv file to google app engine
I´m trying to upload a file which is fetched from a server. But when
im trying to upload a file using org.apache.commons.fileupload. but i dont no, what mistake
I am trying to upload a file or stream of data to our web
I'm trying to upload an application to the iPhone App Store, but I get
I'm trying to upload a file to scribd. It says: The contents of the
I am trying to upload file using php. I am using the function move_uploaded_file.

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.