PHP / MySQL / AJAX - 更新查询不使用会话变量创建记录

时间:2014-12-28 18:51:41

标签: php mysql ajax

我有一个创建任务的页面。用户必须登录才能访问它。我正在使用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的新手,所以如果有人能指引我朝着正确的方向前进,那就太棒了。

感谢。

1 个答案:

答案 0 :(得分:0)

确保你在两个php脚本上都调用了session_start,我的意思是设置$ _SESSION ['userid']的脚本和ajax调用的脚本,即register_process.php