[英]PHP/MySQL/AJAX - Update query doesn't create record using session variable
我有一個創建任務的頁面。 用戶必須登錄才能訪問它。 我正在使用ajax將表單數據發布到php頁面。 在PHP頁面上的表單數據之上,我正在檢查是否設置了會話變量,然后向其分配了一個變量,以后將其用於插入數據庫中。
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();
}
}
我以為查詢失敗,因為通過AJAX進行請求時會話變量(userid)未正確傳遞。 我是PHP,MySQL和AJAX的新手,所以如果有人可以向正確的方向指示我,那就太好了。
謝謝。
確保您已經在php腳本上都調用了session_start,我的意思是設置$ _SESSION ['userid']的腳本和由ajax調用的腳本,即register_process.php。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.