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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:54:56+00:00 2026-06-17T01:54:56+00:00

Below I want to insert values into the database. I want to insert each

  • 0

Below I want to insert values into the database. I want to insert each student within a chosen session(exam).

Now lets say the chosen session is 34, my issue is that lets say a select box contains multiple students who are going to take this assessment like below:

<select name='addtextarea' id='studentadd'  multiple='multiple' size='10'>
<option value='1'>Joe Blogs</option>
<option value='4'>Bill Kite</option>
<option value='6'>Mary Scott</option>
</select>

My question is how can I loop through each student so that it inserts the details for each student in the database so that the db table will look like this below when inserted:

SessionId  StudentId
34         1
34         4
34         6

Below is the code for the insert using mysqli/php:

$studentid = (isset($_POST['addtextarea'])) ? $_POST['addtextarea'] : ''; 
        $sessionid = (isset($_POST['Idcurrent'])) ? $_POST['Idcurrent'] : '';   

        $insertsql = "
        INSERT INTO Student_Session
        (SessionId, StudentId)
        VALUES
        (?, ?)
        ";
        if (!$insert = $mysqli->prepare($insertsql)) {
        // Handle errors with prepare operation here
        }                                           

        $insert->bind_param("ii", $sessionid, $studentid);

        $insert->execute();

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

        $insert->close();
  • 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-17T01:54:57+00:00Added an answer on June 17, 2026 at 1:54 am

    Just loop the bind_param() and execute(), you only need to do the prepare once:

    To prevent inserting zeros, I would also wrap the whole of the prepared statement in something like:

    if(isset($_POST['addtextarea']) && is_array($_POST['addtextarea']) && count($_POST['addtextarea']) > 0 && isset($_POST['Idcurrent']))
    {
        // we have some data to insert, do the prepared stmt
    
        $insertsql = "
        INSERT INTO Student_Session
        (SessionId, StudentId)
        VALUES
        (?, ?)
        ";
    
        if ($insert = $mysqli->prepare($insertsql)) {
    
            foreach($_POST['addtextarea'] as $studentid)
            {
                $insert->bind_param("ii", $_POST['Idcurrent'], $studentid);
    
                $insert->execute();
    
                if ($insert->errno) {
                    // Handle query error here
                }
            }
    
            $insert->close();
        }
        else
        {
            // prepare() call failed - syntax error in query or error with the db
            // Handle errors with prepare operation here
        }
    }
    else
    {
        // post data is missing, show message
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the code Shows below, I want to insert a row into a database
I have an entity Report whose values I want to insert into a database
I have the following code below which works, but I want to insert values
I want to perform an insert into the database so that the database table
I have a list of checkboxes that I want to insert into a database,
I am trying to insert data into MySQL database. For each of the data
The below prepared statement doesn't insert into the database. $sid =1; $sid2 = $GET['sid2'];
I can add multiple rows into my database using the below $sql = 'INSERT
I am attempting to insert data from a dictionary into a database. I want
in a web application, i want to insert the XML file content into an

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.