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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:22:58+00:00 2026-05-26T16:22:58+00:00

I am looking at how to update a specific table depending on whether the

  • 0

I am looking at how to update a specific table depending on whether the ID already exists in that table or whether the record needs to be created.

I am planning on using a function which is passed the id number from the main table in the database. The id value.

so something like update($id). But when this function is called I am not quite clear on how to check if the value already exists in the database, if it does then update the record. If it doesn’t then create the record. Any thoughts?

I understand how to update the table, but not how to use the conditional statements I require.

$query = "UPDATE reports FROM them where id='$id'";

PSEUDOCODE:

  1. Check if $id exists in table.
  2. If $id exists, update that row
  3. If $id does not exists, create the record.
  • 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-26T16:22:58+00:00Added an answer on May 26, 2026 at 4:22 pm

    You need to execute a select statement beforehand to check whether a record with that id already exists. If the results from executing the query are not empty, then use an UPDATE statement. If the results are empty, call INSERT instead.

    To check for existing records, use the query like the one below. Code sample provided.

    SQL:

    "Select * from reports where id='$id'";
    

    PHP:

    $result = mysql_query("Select * from reports where id='$id'");
    if (mysql_num_rows($result) > 0) {
       mysql_query("UPDATE...(your update statement query string)");
    } else {
       mysql_query("INSERT...(your insert statement query string)");
    }
    

    Note that this is a basic example, and you should remember to do things like sanitize your query variables to prevent SQL injection, as well as check for error conditions whenever executing a query.

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

Sidebar

Related Questions

We have a table that needs to be locked down in a specific way
Looking for a good tutorial on how to update a mysql database using a
I'm looking for a way to create an online form that will update an
I'm looking to update certain rows based on specific criteria based on other rows.
I'm looking to create a view of a table which will highlight data that
I'm looking to update our project's jaxb version from 1 to 2. Has anyone
I have a legacy service I'm looking to update to WCF and one of
Looking at all the possibilites of creation / update columns in NHibernate I mostly
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I am looking to parse INSERT and UPDATE MySQL SQL queries in PHP to

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.