0

I read similar answer here in this question: How to insert into MYSQL row from multiple $_POST arrays and How to insert into MYSQL row from multiple $_POST arrays but the problem is these answers do not work in my code. Is it because im using an ajax? and i only get the value of the first array.

If i also place the variable declaration inside the for loop it is not working too.

Here is my ajax:

var name = []; 
$('input[name="name[]"]').map(function(){ name.push($(this).val()); }); var studid = []; 
$('input[name="studid[]"]').map(function(){ studid.push($(this).val()); }); var nameStr = name != '' ? '&name='+ name : '';
var studStr = studid != '' ? '&studid='+ studid : '';
var dataString = 'subject='+ subject + '&section=' + section + studStr + nameStr;

$.ajax({ type: "POST", url: 'save.php', data: dataString, dataType: "html", 
success: function(data) {
    $('input#subject-field').val('');
    $('input#section-field').val('');
    $('input.record-input-forms').val('');
    $('#status-message').css({"color":"#39b1c6"});
    $('#status-message').html('Save successfully',function(){
    $('#status-message').fadeOut(2000); }); }, 
error:function (xhr, ajaxOptions, thrownError){
    alert(thrownError); } });
    return false;
}); 

Here is my php:

if(isset($_POST['studid']) || isset($_POST['name'])){
    $studid = array_map(mysql_real_escape_string, explode(",",$_POST['studid']));
    $name = array_map(mysql_real_escape_string, explode(",",$_POST['name']));   

    for ($i=0; $i<count($studid); $i++){
        $sql_1 = "INSERT INTO tbl_student(StudentID, StudentName, SubjectID)  VALUES ('".$studid[$i]."', '".$name[$i]."', LAST_INSERT_ID())"; 
        mysqli_query($con,$sql_1);  
    } 
}

2 Answers 2

1

use mysql_insert_id(); instead of LAST_INSERT_ID()

Sign up to request clarification or add additional context in comments.

1 Comment

this is part of the solution i had, there is nothing wrong with the ajax. It was my bad, i set he subject id as foreign key in the table student.. I change my syntax last_insert_id() to that above and now it is working. Thank you.
0

You're not sending data correctly from the jQuery and its seems you'r mixing arrays and string together.

This is a simple request that posts studid-array from jQuery

var saveData = $.ajax({
                    type: 'POST',
                    data: {studid: studid},
                    url: 'save.php',
                    dataType: 'html'
                });

saveData.done(function(data) {                                                   
    $('input#subject-field').val('');
    $('input#section-field').val('');
    $('input.record-input-forms').val('');
    $('#status-message').css({"color":"#39b1c6"});
    $('#status-message').html('Save successfully',function(){
    $('#status-message').fadeOut(2000); });

});                        

saveData.fail(function(ts) {
    alert(ts.responseText);
});

When save.php is called, $_POST['studid'] would be set (if there are anything in the array)

If you instead do like this:

var saveData = $.ajax({
                        type: 'POST',
                        url: 'save.php?studid=' + studid,
                        dataType: 'html'
                    });

When save.php is called, $_GET['studid'] would be set (if there are anything in the array). The best way though is to use data-option in the ajax-function call (in my first case). If you choose to use this option you would have to serialize the stuid-array before putting it in as a part of an url.

UPDATE If you want to pass multiple arrays you would have to do something like this:

var saveData = $.ajax({
                        type: 'POST',
                        data: {studid: studid, name_arr2: data_arr2},
                        url: 'save.php',
                        dataType: 'html'
                    });

6 Comments

thank you for the response, this was actually inrelation with my question here : stackoverflow.com/questions/28665983/… it is actually working if i only work with one array and foreach to loop with each array, there was no issue in sending the data to the database. Im just stumble down when i tried to add another array in my request which is the var $name.,since foreach can only handle one parameter so i tried using forloop but i cant figure it out.
If you have two array you want send with data-option just do like this: data: {name of arr1: data from arr1, name of arr2: data from arr2}
but how about my php is it coded right? im still new in using ajax, and im not yet that good. i was hoping a more straight forward answer. But thank you, i will try to configure the answers you provided. :)
@rigorcadiz - A more straightforward answer? "these answers do not work in my code.", "I only get the value of the first array.", "If i also place the variable declaration inside the for loop it is not working too.". ---- I see that your code is not working, but start from the basics... what do you want to achieve with your code?
i want to know, what im doing wrong with my codes above especially in php. Why i cant get the value of each array using a for loop, if im sending the data incorrectly from jquery/ajax then why it is working if use foreach? for instance if i will only use one array. I can save the value of each array from variable $studid alone.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.