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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:01:23+00:00 2026-06-14T15:01:23+00:00

I have simple insert/update page where I do check if there is id, if

  • 0

I have simple insert/update page where I do check if there is “id”, if no then proceed insert query otherwise proceed update query, and this works fine. Problem is that I need that id for the next query, in case of Insert query I will get it as “lastInsertId” but in case of Update query “lastInsertId” will overwrite $_POST[“id”].

Is there a way to identify which query has been performed (insert or update) and then get right id, or I have to bind/ execute query’s separately?

$id= $_POST["id"];  
  try { 
    $DBH = db_connect ();
    $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
      if ($id== "") {
         $query= $DBH->prepare('INSERT INTO table (x1,x2) VALUES(:x1,:x2)');                        
      } else {
         $query= $DBH->prepare('UPDATE day SET x1=:x1, x2=:x2 WHERE id= :id'); 
           $query-> bindValue(':id', $id);
      }

           $query-> bindValue(':x1', $x1);
           $query-> bindValue(':x2', $x2);                                                                  
           $query-> execute();                              

        $id= $DBH-> lastInsertId('id') ; //get last inserted ID
    } 
  • 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-14T15:01:24+00:00Added an answer on June 14, 2026 at 3:01 pm

    You can use exactly the same if clause as for picking the insert query as the query to excecute. Then your code would look like this:

    $id= $_POST["id"];  
        try { 
          $DBH = db_connect ();
          $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
          if ($id== "") {
             $query= $DBH->prepare('INSERT INTO table (x1,x2)     VALUES(:x1,:x2)');
          } else {
             $query= $DBH->prepare('UPDATE day SET x1=:x1, x2=:x2 WHERE id= :id'); 
             $query-> bindValue(':id', $id);
          }
    
           $query-> bindValue(':x1', $x1);
           $query->bindValue(':x2',$x2);                                                                  
           $query-> execute();
    
        if($id==""){
            $id= $DBH-> lastInsertId('id') ; //get last inserted ID
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a pretty simple problem. Inserting data into the table normally works fine,
Hopefully I'm just missing something and this is simple... I have a page that
I don't think this is a simple LIMIT query. I have organisers that have
I have a Simple insert and update test for a table: case class Player(id:
I have simple SSIS package which reads data from flat file and insert into
I have a method which does a simple mysql insert, when I tried to
So I have a simple method called Invert : public static void Invert(this bool
Simply what I need to do is to have a page where there is
In my webapp I have a page called display.php. The script in this page
I have a simple JQuery enabled ASP.NET page: <script> $(document).ready(function() { $(#accordion).accordion(); }); </script>

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.