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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:18:21+00:00 2026-06-16T22:18:21+00:00

Below I have an INSERT database function using php and mysqli: for ($i =

  • 0

Below I have an INSERT database function using php and mysqli:

for ($i = 1, $n = $_SESSION['sessionNum']; $i <= $n; $i++) {

        $insertsql = "
  INSERT INTO Session
    (SessionName, SessionTime, SessionDate, SessionWeight, SessionDuration, TotalMarks, ModuleId, TeacherId, Room)
  VALUES
    (?, ?, ?, ?, ?, ?, ?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
  // Handle errors with prepare operation here
}

    $sessname = $_SESSION['id'] . ($n == 1 ? '' : $i);
    $sessdate = date("Y-m-d", strtotime($_SESSION['dateChosen']));

    $insert->bind_param("sssisiiis", $sessname, $_SESSION['timeChosen'], $sessdate,
                 $_SESSION['totalWeight'], $time, $_SESSION['textMarks'],
                 $_SESSION['hiddenmodule'], $teacherid, $_SESSION['rooms']);

    $insert->execute();

    if ($insert->errno) {
      // Handle query error here
    }

    $insert->close();

}

The above code will insert this data in the database below as an example in one go:

SessionId (Auto) SessionName  SessionTime  //etc
1                ADFGR1       01:00:00
2                ADFGR2       13:00:00
3                ADFGR2       09:00:00

Anyway you can see I have not include SessionId in the code above as it is an auto increment meaning that it is going to be displayed anyway.

But the problem I have is that underneath this insert, what I want to do is that when the above insert is finished, I want to do another insert underneath:

        $insertsession = "
  INSERT INTO Session_Complete
    (SessionId)
  VALUES
    (?)
";
if (!insertdb = $mysqli->prepare($insertsession)) {
  // Handle errors with prepare operation here
}


    insertdb->bind_param("i",$value);

    insertdb->execute();

    if (insertdb->errno) {
      // Handle query error here
    }

But the problem I have with the above insert is that it requires the already inserted SessionId‘s from the first insert in order to insert those ids into its table. My question is how do I retrieve the SessionId from the first insert so I can include them in the second insert?

  • 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-16T22:18:22+00:00Added an answer on June 16, 2026 at 10:18 pm

    it should be this:

    $insert->insert_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have this method below which should insert values into my database. However
I have below code to insert a style into DOM (there is a use
I have a query like below declare @str_CustomerID int Insert into IMDECONP38.[Customer].dbo.CustomerMaster ( CustomerName
I have a byte array highlighted below, how do I insert it into a
Below is part of a PHP database class someone else wrote, I have removed
I have a php code snippet like below : function is_ancestor_of( $page_name = null,
I have a tiny class of inserting payment record into database. below is given:
I am trying to do a simple insert into MySQL. I am using mysqli
I am using the code below to insert the values from the form into
I have a database listed as $db under mysqli. This database is contains into

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.