Jacques
Jacques

Reputation: 3774

Javascript is posting variables but all my data is not input into database

I have a MySQL database that I am adding records to through a form using AJAX. Console is telling me that all my data is being posted to the php page, and I'm not getting any error. However, not all the data is showing up in the database.

PHP:

    //set values sent from javascript
        $evaluator = $_SESSION['id'];
        $feedback = $_POST['feedback'];
        $student_work = $_POST['student_work'];
        $student = $_POST['student'];
        $assessment = $_POST['assessment'];
        $classwork = $_POST['classwork'];
        $best = $_POST['best'];
        $subject = $_POST['subject'];
        $binder = $_POST['binder'];
        $teacherid = $_POST['teacher'];
        $formid = $_POST['form'];
        $draft_flag = $_POST['s'];
        //set draft time to use for saving/updating drafts
        $draft_time = date("Y-m-d H:i:s");
        //flag that decided what we're saving, ie draft, submit, update.
        $flag =   $_POST['draft'];
        //Querys the database to get the form name
        $fquery = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT form_name FROM forms WHERE id_form='$formid' LIMIT 1");
        $row = mysqli_fetch_assoc($fquery);
        $form_name  = $row['form_name'];
        //query's the database to get the teacher's school for this evaluation
        $tquery = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT id_school FROM teachers WHERE teacher_id='$teacherid' LIMIT 1");
        $trow = mysqli_fetch_assoc($tquery);
        $id_school = $trow['id_school'];

        include_once 'functions/functions.php';

if ($draft_flag == "submit"){

//inserts collected data and sets status as submitted in the evaluations table
$query = "INSERT INTO evaluations SET 
        id_user = '$evaluator',
        time_submitted = LOCALTIME(),
        date_submitted = NOW(),
        feedback = '$feedback', 
        student_work_org = '$student_work',
        best_works_port = '$best',
        binder = '$binder',
        assesment_folders = '$assessment',
        classwork_organization = '$classwork',
        student_desks = '$student',
        subject_specific_rubric = '$subject',
        id_teacher = '$teacherid',
        id_school = '$id_school',
        id_form = '$formid',
        status = 'Submitted'";

mysqli_query($GLOBALS["___mysqli_ston"], $query); 
?>

Out of all the variables, these are not posting:

$feedback = $_POST['feedback'];
$student_work = $_POST['student_work'];
$student = $_POST['student'];
$assessment = $_POST['assessment'];
$classwork = $_POST['classwork'];
$best = $_POST['best']; 
$subject = $_POST['subject'];
$binder = $_POST['binder'];

That's 8 of the 12 not posting.

EDIT: Here is what is sent by the js:

ajax.send(
            "classwork=" + classwork
            + "&student_work=" + student_work
            + "&feedback=" + feedback
            + "&assessment=" + assessment
            + "&best=" + best
            + "&student=" + student
            + "&subject=" + subject
            + "&binder=" + binder
            + "&teacher=" + teacher
            + "&form=" + form
            + "&draft=" + draft
            + "&s=" + s
            );

I have several other forms just like this that are working fine. Thanks in advance for the help!

EDIT:

Please do not tell me my syntax is incorrect, unless you're going to tell me I missed a comma or something. (which I checked for, so I doubt that's the problem :) ) I know it may not be the way YOU would INSERT, but it's correct according to http://dev.mysql.com/doc/refman/5.7/en/insert.html

Just tried and it did the same thing.

$query = "INSERT INTO evaluations (
        id_user, 
        time_submitted, 
        date_submitted, 
        feedback,  
        student_work_org, 
        best_works_port, 
        binder, '$binder',
        assesment_folders, 
        classwork_organization, 
        student_desks, 
        subject_specific_rubric, 
        id_teacher, 
        id_school, 
        id_form, 
        status) 
VALUES (
        '$evaluator',
        LOCALTIME(),
        NOW(),
        '$feedback',
        '$student_work',
        '$best',
        '$assessment',
        '$subject',
        '$classwork',
        '$student',
        '$teacherid',
        '$id_school',
        '$formid',
        'Submitted'
) ";

Upvotes: 0

Views: 128

Answers (1)

federicot
federicot

Reputation: 12341

Your INSERT syntax is wrong. This is the correct form:

INSERT INTO <table name> (
    <field name foo>
    <field name bar>
    .
    .
    .
)
VALUES (
    <value for foo>,
    <value for bar>,
    .
    .
    .
)

Upvotes: 1

Related Questions