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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:23:20+00:00 2026-05-18T04:23:20+00:00

I have a table in a mySQL database we’ll call ‘tbl’ where the fields

  • 0

I have a table in a mySQL database we’ll call ‘tbl’ where the fields are:

id, userID, favorite, emailID

The id is auto incremental. The userID stores an integer. The favorite is either yes or no. The emailID stores an integer.

I am programing in PHP and would like to have one query used to query the database that checks if the userID and emailID combination exists. If it does then update the favorite field with a yes or no value that comes from a form that is passed into the query dynamically. If it does not exist then insert the combination into the database.

Therefore if I had:

<?php
$userID = 34;
$emailID = 395;
$favorite = "yes"; // could be yes or no.

I don’t believe the query below is correct but gets the idea of what I am trying to do:

IF NOT EXISTS 
(SELECT userID, favorite, emailID 
FROM tbl 
WHERE ((userID = '$userID')and(emailID = '$emailID')) 
INSERT INTO tbl (userID, favorite, emailID) VALUES ('$userID', '$favorite', '$emailID')
ELSE UPDATE tbl
SET favorite = '$favorite'
WHERE ((userID = '$userID')and(emailID = '$emailID')) 

(please know that I know that entering values into a query like this is a security risk, this is only to help explain my question.)

What is the best way to write the query? Can it be written with one query?

  • 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-18T04:23:21+00:00Added an answer on May 18, 2026 at 4:23 am

    I would drop the redundant id column and define the primary key as the combination of (userID, emailID), which means you have exactly one record for each different combination of the two fields, which seems to be what you want. Then you can use the REPLACE INTO command, which is equivalent to an INSERT but deletes any existing record if the primary key matches.

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

Sidebar

Related Questions

I have a table in a mysql-database with the fields name, title, cd_id, tracks
I have an order table in MySQL database, having a field/column which stores the
I have a user table in my mysql database that has a password column.
I have a MySQL database table with a couple thousand rows. The table is
I have a table in MySQL that has 3 fields and I want to
I have a table in my Mysql database, which is used for authentication. And
I have a table in a MySql database with a datetime field. This is
I have a table in a MySQL database from which I want to select
I have a table in my MySQL database that looks like the following banner
i have a table in my mysql database named (names) now everyone can save

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.