dpzbzp8728 2014-12-28 18:51
浏览 49
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • 「已注销」 2014-12-28 19:08
    关注

    be sure you have called session_start on both php script, i mean the script that set $_SESSION['userid'] and script called by ajax, that is register_process.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题