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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:05:45+00:00 2026-06-06T09:05:45+00:00

I am trying to prevent the insertion of duplicate rows into mysql database. The

  • 0

I am trying to prevent the insertion of duplicate rows into mysql database. The table name is favorites and I have two columns in my table: company_id and user_id, I want to prevent users to try to add the same company to the db as a ‘favorite’ twice.

This is what I tried:

$query  = "INSERT IGNORE INTO favorites (item_id, user_id) VALUES ( $item_id, $user_id )";
    mysql_query($query,$conn);

But does not work.

I also tried to ‘alter table’ to add a primary key, however, I need both user_id and item_id to be keys, because the same favorited item can be favorited by more than one ‘user_id’ and the same ‘user_id’ can insert many different favorited items, so that data can be ‘duplicated’ but I am trying to prevent the exact same ‘user_id’ and ‘item_id’ to be inserted twice.

I appreciate any help with this.

  • 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-06T09:05:46+00:00Added an answer on June 6, 2026 at 9:05 am

    You can use a composite primary key of the columns like so:

    ALTER TABLE table ADD PRIMARY KEY ( 'item_id' , 'user_id' )
    

    This means that same user_ids and item_ids are allowed and only a combination of them needs to be unique.

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

Sidebar

Related Questions

I got table with 3 columns (Primary)id, type, name. I am inserting data into
I am trying to prevent duplicate entries into my sqlite db by using the
I'm trying to prevent certain keys from being entered into an input box, but
I am trying to prevent VS from breaking on JS errors. I have the
Hi I have a mysql table having the following fields A INT, B INT,
I have a process that is inserting data into a database (SQL Server 2008)
I have two processes that work with data in the same table. One process
I'm trying to prevent user from inserting * in a textbox. This is what
I am trying to prevent any SQL injection in all my queries and would
I am trying to use sp_executesql to prevent SQL injection in SQL 2005, I

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.