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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:58:12+00:00 2026-05-27T09:58:12+00:00

I am trying to get auto increment ID from the last query and update

  • 0

I am trying to get auto increment ID from the last query and update the same row. Also I would like to keep everything in the transaction and throw user friendly error if something goes wrong. Could you please help?

    $parentSQL = "INSERT INTO TABLE1(test1, test2) VALUES ('" .$test1. "', '" .$test2. "')";
$result = mysql_query($parentSQL) or die(mysql_error());

$newId = MySql_Insert_Id();

$val = "MU".$newId;

$updateSQL = "UPDATE TABLE1 SET test3 = '" .$val. "' WHERE id= ".$newId." ";

$updateResult = mysql_query($updateSQL) or die(mysql_error());

$childSQL = "INSERT INTO TABLE2(testId, testCol) VALUES (" .$newId. ", '" .$testCol. "')";

$childResult = mysql_query($childSQL) 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-27T09:58:13+00:00Added an answer on May 27, 2026 at 9:58 am

    This is just a suggestion, but why don’t you try using stored procedures instead?

    In MySQL

    DELIMITER //
    CREATE PROCEDURE test (
        IN param1 VARCHAR(20)
        , IN param2 VARCHAR(20)
        , IN param3 VARCHAR(20)
        , IN param4 VARCHAR(20)
    )
    BEGIN
        INSERT INTO TABLE1(test1, test2) VALUES (param1, param2);
    
        UPDATE TABLE1 SET test3 = CONCAT(param3, LAST_INSERT_ID());
    
        INSERT INTO TABLE2(testId, testCol) VALUES (LAST_INSERT_ID(), param4);
    END//
    DELIMITER ;
    

    In PHP, and using PDO to utilize prepared statements:

    $q = $db->prepare("CALL test (?,?,?,?)");
    $q->bindParam(1, $test1);
    $q->bindParam(2, $test2);
    $q->bindParam(3, $val);
    $q->bindParam(4, $testCol);
    $q->execute();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I'm having problems trying to get a query like this table answers CREATE TABLE
I found several examples of how to get the last inserted row id from
Trying to get my css / C# functions to look like this: body {
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
I am just trying to get the auto incremented value of a table that
I'm trying to delete a record from the GAE datastore via an ajax query
I am trying to make some sort of SQL Query where I only get
I am trying to get my all categories and sub-categories from MySQL database in

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.