AJAX调用不会更新数据库

时间:2017-10-31 10:24:05

标签: javascript ajax

我正在创建一个系统,在其中的一部分,您必须能够从周六轮班中删除员工。为此,您单击一个调用JavaScript函数“removeEmpFromSaturday”的图标并提交相应的参数。

在该脚本中,它应该通过Ajax请求更新值,该请求应该更新我的数据库并从他/她的周六班次中删除该员工。

但是,我指向的PHP页面实际上从未被调用/请求过(“PHP页面上的警报证明了这一点”)。

我对AJAX比较陌生,也许我的语法对于这个函数来说是完全错误的,所以任何额外的指针都会非常感激。

作为参考,当我提醒任何值“id,loc,week,year”时,它们都会给我正确的值,所以这不是问题。

以下是我认为存在问题的代码:

<script>

function removeEmpFromSaturday(id, loc, week, year){

    xhttp = new XMLHttpRequest();
    xhttp.open("GET", "includes/ajax/remove_emp_from_saturday.php?e_id=" + id + 
                      "&location=" + loc + "&week=" + week + "&year=" + year, false);
    xhttp.send();
    resetPlanner();
}

</script>

我指的PHP代码:

<?php require_once dirname(__FILE__)."/../admin_header.php" ;?>

<script>alert("STARTED");</script>

<?php

if(isset($_REQUEST['e_id'])){
    $emp_id = escape($_REQUEST['e_id']);
    $loc = escape($_REQUEST['location']);
    $week = escape($_REQUEST['week']);
    $year = escape($_REQUEST['year']);

    $query = "SELECT e_hp_daily_pat FROM employees WHERE e_id = '{$emp_id}' ";
    $get_hp_daily_pat_query = mysqli_query($connection, $query);
    $row = mysqli_fetch_assoc($get_hp_daily_pat_query);
    $e_hp_daily_pat = escape($row['e_hp_daily_pat']);

    $query = "SELECT * FROM slots WHERE s_location = '{$loc}' AND s_day = '6' 
        AND s_week = '{$week}' AND s_year = '{$year}' ";
    $get_emps_query = mysqli_query($connection, $query);
    $row = mysqli_fetch_assoc($get_emps_query);
    $s_real_sub = escape($row['s_real_sub']);
    $s_emps = escape($row['s_emps']);
    $s_emps = explode(";", $s_emps);
    $process = false;
    $e_match = 0;
    foreach($s_emps as $emp){
        if($emp == $emp_id){
            unset($s_emps[$e_match]);
            $process = true;
          }
        $e_match++;
    }

    if($process == true){
        $s_emps = implode(";", $s_emps);
        $s_real_sub -= $e_hp_daily_pat;
        $query = "UPDATE slots SET s_emps = '{$s_emps}', s_real_sub = '{$s_real_sub}' WHERE s_location = '{$loc}' AND s_day = '6' AND s_week = '{$week} AND s_year = '{$year}' ";
        $set_emps_query = mysqli_query($connection, $query);
      }
    }
?>

<script>alert("COMPLETE");</script>

请在任何人提到它之前,我知道我没有约束我的参数,而且在mysqli中稍微过时了,我稍后会更新。

1 个答案:

答案 0 :(得分:1)

您的更新查询中有错误:

   $query = "UPDATE slots SET s_emps = '{$s_emps}', s_real_sub = '{$s_real_sub}' WHERE s_location = '{$loc}' AND s_day = '6' AND s_week = '{$week} AND s_year = '{$year}' ";

s_week

之后你错过了一个“'”
   $query = "UPDATE slots SET s_emps = '{$s_emps}', s_real_sub = '{$s_real_sub}' WHERE s_location = '{$loc}' AND s_day = '6' AND s_week = '{$week}' AND s_year = '{$year}' ";

一般建议:调试你的php代码,尽量不要使用<script>alert("")</script>,而是直接在php中使用echo调用。

这将确保找到并解析php。