简体   繁体   中英

PHP/MySQL/AJAX - Update query doesn't create record using session variable

I have a page which creates a task. The user has to be logged in to access it. I'm using ajax to post the form data to a php page. On top of the form data on the PHP page I'm checking if a session variable is set and then assigning a variable to it which I later use to insert into the database.

AJAX:

<script>
$(document).ready(function() {
$("#FormSubmit").click(function (e) {
e.preventDefault();

var hasError = false;

task_name = $("#task_name").val();
if(task_name === '') {
    $("#error").empty().append("Please enter task name.");
    $("#task_name").css("border-color", "#FF5454");
    hasError  = true;
    return false;
} else {
    $("#error").hide();
    $("#task_name").css("border-color", "#4DC742");
}

task_notes = $("#task_notes").val();

task_duedate = $("#datepicker1").val();
if(task_duedate === '') {
    $("#error").show();
    $("#error").empty().append("Please enter a task due date.");
    $("#datepicker1").css("border-color", "#FF5454");
    hasError  = true;
    return false;
} else {
    $("#error").hide();
    $("#datepicker1").css("border-color", "#4DC742");
}

task_category = $("#task_category").val();
if(task_category === '') {
    $("#error").show();
    $("#error").empty().append("Please select a task category.");
    $("#task_category").css("border-color", "#FF5454");
    hasError  = true;
    return false;
} else {
    $("#error").hide();
    $("#task_category").css("border-color", "#4DC742");
}

if(hasError == false){
jQuery.ajax({
type: "POST",
url: "http://test.student-portal.co.uk/includes/register_process.php",
data:'userid=' + userid + '&task_name=' + task_name + '&task_notes=' + task_notes + '&task_duedate=' + task_duedate + '&task_category=' + task_category,
success:function(response){
    $("#hide").hide();
    $("#register-button").hide();
    $("#FormSubmit").hide();
    $("#error").hide();
    $("#success").append('Task created successfully. To create another task, simply fill in the form again.');
    $("#success-button").show();
},
error:function (xhr, ajaxOptions, thrownError){
    $("#error").show();
    $("#error").empty().append(thrownError);
}
});
}

return true;

});
});
</script>

PHP:

if (isset($_SESSION['userid']))
$userid = $_SESSION['userid'];
else $userid = '';

if (isset($_POST['task_name'], $_POST['task_notes'], $_POST['task_duedate'], $_POST['task_category'])) {

$task_name = filter_input(INPUT_POST, 'task_name', FILTER_SANITIZE_STRING);
$task_notes = filter_input(INPUT_POST, 'task_notes', FILTER_SANITIZE_STRING);
$task_duedate = filter_input(INPUT_POST, 'task_duedate', FILTER_SANITIZE_STRING);
$task_category = filter_input(INPUT_POST, 'task_category', FILTER_SANITIZE_STRING);

// Check existing task name
$stmt1 = $mysqli->prepare("SELECT userid FROM user_tasks WHERE task_name = ? LIMIT 1");
$stmt1->bind_param('s', $task_name);
$stmt1->execute();
$stmt1->store_result();
$stmt1->bind_result($db_userid);
$stmt1->fetch();

if ($stmt1->num_rows == 1) {
header('HTTP/1.0 550 A task with this task name already exists.');
exit();
$stmt1->close();
} else {

$task_status = 'active';

$stmt2 = $mysqli->prepare("INSERT INTO user_tasks (userid, task_name, task_notes, task_duedate, task_category, task_status) VALUES (?, ?, ?, ?, ?, ?)");
$stmt2->bind_param('isssss', $userid, $task_name, $task_notes, $task_duedate, $task_category, $task_status);
$stmt2->execute();
$stmt2->close();

}

}

I'm thinking the query is failing because of the session variable (userid) is not getting passed properly when I do the request through AJAX. I'm new to PHP, MySQL and AJAX so it'd be great if someone could direct me in the right direction.

Thanks.

确保您已经在php脚本上都调用了session_start,我的意思是设置$ _SESSION ['userid']的脚本和由ajax调用的脚本,即register_process.php。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM