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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:11:31+00:00 2026-06-17T08:11:31+00:00

I want to insert a sessionid and a set of students into the db.

  • 0

I want to insert a sessionid and a set of students into the db.

The sessionid can be found in line of code below:

<td><input type='hidden' id='currentId' name='Idcurrent' readonly='readonly' value='4' /> </td>

A set of students are displayed in a select box as so:

<select id="studentadd" name="addtextarea">
<option value='1'>u08743 - Joe Cann</option>
<option value='4'>u03043 - Jill Sanderson</option>
<option value='7'>u08343 - Craig Moon</option>
</select>

Now I am using ajax to get both the sessionId value and the student’s value and post them into a seperate php page where the insert happens by using code below:

 function submitform() {    

    $.ajax({
        type: "POST",
        url: "updatestudentsession.php",
data: { 
    Idcurrent:  $('#currentid').val(),
    addtextarea:$('#studentadd').val()
        },
        dataType:'json',  //get response as json
        success: function(result){
                    if(result.errorflag){

      $("#targetdiv").html('success');  

    }else{
       //you got success message

            $("#targetdiv").html('error');

    $('#targetdiv').show();
        }
    }
  });        
}

Below is the seperate php file updatestudentsession.php where it is suppose to do the insert:

$studentid = array();
$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
}                                       

foreach($studentid as $id)
{    

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

$insert->execute();

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

}

$insert->close();

Now the problem I am having is that is not inserting the correct data in at all. It is insert the value 0 in both the SessionId and StudentId fields. Below is what the table should look like after the insert:

SessionId   StudentId
4           1
4           4
4           7

Instead it looks like below:

SessionId   StudentId
0           0

My question is what is causing it to not be able to retrieve and insert the correct data into the db?

  • 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-17T08:11:32+00:00Added an answer on June 17, 2026 at 8:11 am

    Suppose part of problem is here:

    Idcurrent:  $('#currentid').val(),
    

    In your HTML code ID is currentId when you are trying to get an element with id currentid. id selector is case sensitive. So, you simply pass nothing to your PHP as jquery can’t find an element.

    Not sure what is wrong with $studentid, but I see no reason to use an array there as you always pass only one value, so try to change your php code like below:

    $studentid = (isset($_POST['addtextarea'])) ? $_POST['addtextarea'] : array();  
    $sessionid = (isset($_POST['Idcurrent'])) ? $_POST['Idcurrent'] : '';   
    //var_dump($studentid);
    //var_dump($sessionid);
    //var_dump($_POST); - this is additionally to see whole $_POST variable.
    $insertsql = "
    INSERT INTO Student_Session
    (SessionId, StudentId)
    VALUES
    (?, ?)
    ";
    if (!$insert = $mysqli->prepare($insertsql)) {
    // Handle errors with prepare operation here
    }                                       
    
    
    foreach($studentid as $id)
    {    
       $insert->bind_param("ii", $sessionid, $id);
       $insert->execute();
       if ($insert->errno) {
          // Handle query error here
       }
    } 
    
    $insert->close();
    

    Also, see at commented out var_dump lines at the beginning. They will print values of your variables so you will be able to see what is actually set there. (as it is ajax, you will need to open developer tools and see what is returned in result on network tab)

    UPD:

    According to the latest comment, appears that$_POST['addtextarea'] is an array. Because of that code is modified to handle that. Please note how that value is taken:

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

    Old code, which is:

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

    results in two level array. So $studentid is an array which contains one element which is an array also. That is why old for each loop is not working. $id were an array. And it was converted to 0

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

Sidebar

Related Questions

I want insert a custom snippet into an XML file from code within an
I want to insert a record into a MySQL database table from a formulaire.
I want to insert the following include tag into my webpage using JavaScript. <!--#include
I want to insert widgets into my ItemsControl and make them resizeable. How do
i want to insert all processes that running into listbox, and also how to
I have a trigger that I want to run before insert a row into
UPDATE `comment` SET `agree`=`agree`+1,(INSERT INTO `reacted_on` VALUES (10,197)) WHERE `id`=197 and 0 = (SELECT
I want to insert a date record to an Access database. Here is my
I want to insert given text to existing pdf at X,Y location provided. I
I want to insert some items to mysql db. When I did it 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.