PHP - SQL - 按钮上的更新数据单击

时间:2017-12-18 04:08:40

标签: javascript php jquery html mysql

此问题的创建是因为我之前的问题包含2个问题,而不是将其缩小为1

目标

当用户选择三个变量来访问数据时,用户就可以点击按钮来更改该数据的其中一个细节。

SQL - 数据库表(SchoolData)

+-----+--------+------------+------------+----------------+-----------+
|  ID |  Class |   Teacher  |  YearMonth |   Description  |   Status  |
+-----+--------+------------+------------+----------------+-----------+
|  1  |  Alpha |    Sara    |  2017/01   |  Good & Clean  |  Pending  |
+-----+--------+------------+------------+----------------+-----------+
|  2  |  Alpha |    Sara    |  2017/01   |  Has 30 Chairs |  Pending  |
+-----+--------+------------+------------+----------------+-----------+
|  3  |  Alpha |    Sara    |  2017/01   |  Has 30 Tables |  Pending  |
+-----+--------+------------+------------+----------------+-----------+
|  4  |  Alpha |    Sara    |  2017/01   |   5 Subjects   |  Pending  |
+-----+--------+------------+------------+----------------+-----------+
|  5  |  Beta  |    John    |  2016/11   |   Big & Clean  |  Official |
+-----+--------+------------+------------+----------------+-----------+
|  6  |  Beta  |    John    |  2016/11   |   New Student  |  Official |
+-----+--------+------------+------------+----------------+-----------+
|  7  |  Beta  |    John    |  2016/11   | Injured Student|  Official |
+-----+--------+------------+------------+----------------+-----------+
|  8  |  Beta  |    John    |  2016/11   |   6 Subjects   |  Official |
+-----+--------+------------+------------+----------------+-----------+

网页

+---------------------------------------------------------------+
|                                                               |
| |>Select Class<|   |>Select Teacher<|   |>Select Year/Month<| |
|                                                               |
|                         (Search)                              |
|                                                               |
|                                                               |
|>>>>>>>>>>>>>>>>>>>>>>>>>>INPUT<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|
|                                                               |
|      {Class}           {Teacher}           {Description}      |
|                                                               |
|                                                               |
|                        (Update)                               |
|                                                               |
+---------------------------------------------------------------+
  • 图例

    下拉列表 - |&gt;&lt; |

    按钮 - ()

    文字输入 - {}

网页描述

更新按钮将更改&#34;状态&#34;所选数据到&#34;待定&#34;。因此,如果让我们说用户搜索| Beta |约翰| 2016/11 |并点击更新按钮,&#34;状态&#39;因为该数据将从&#34;官方&#34;到&#34;待定&#34;

但是,如果工作人员搜索的数据是&#34;状态&#39;已经&#34;待定&#34;例如,| Alpha |萨拉| 2017 |,更新按钮将被禁用。

HTML表格

<!--Those data are setting for checking data existance in the database -->
<input type="hidden" name="class" value=" <?php echo $class;  ?>" >
<input type="hidden" name="date" value=" <?php echo  $getDate;  ?>">
<input type="hidden" name="teacher" value=" <?php echo  $teacher;  ?>" >
<input type="hidden" id="inputStatus" name="status" value="">

更新按钮(HTML)

<td colspan="1" valign="bottom" align="left">
    <button type="button" class="btn btn-lg update" id="btnUpdate" name="update"> Enable Re-Submit </button>
</td>

更新按钮功能(JavaScript)

$(function (){
    $("button#btnUpdate").on('click', function (e){
        e.preventDefault();
        $("#inputStatus").val("update");
        $.ajax({
            type: 'post',
            url: 'changeClassStatus.php'
        });
    }
}

changeClassStatus.php(PHP)

<?php
  $class  = trim($_POST['calss']);
  $teacher = trim($_POST['teacher']);
  $date   = $_POST['date'];
  $date   = $date."/01";
  $status = $_POST['status'];
  $empId = $_POST['empId'];

    if($status == 'update'])){
        $sqlUpdate = mysqli_query($conn,"UPDATE SchoolData SET Status='Pending' WHERE (Class=$class AND Teacher=$teacher AND ID='".$id[$i]."' AND YEAR(MonthYear)= YEAR('$date') AND MONTH(MonthYear)= MONTH('$date'));";
        $sqlExecute=$conn->query($sqlUpdate)or exit("Error code ({$conn->errno}): {$conn->error}");

        echo "Data Status Changed";
    }
?>

问题说明

我已经实现了代码,但他们没有更新搜索数据的状态。当我点击更新按钮时,数据不会更新且没有任何反应(无更改和无回显消息)

附加说明

此问题与PHP - SQL - Disable, Hide or Enable Button based on Data相关联。如果我的问题有任何缺乏或不准确的信息或问题,请告诉我。谢谢

3 个答案:

答案 0 :(得分:1)

在你的If语句中,让我们开始,但试着看看你的所有变量是否正确地传递到你的查询中,你可以通过编写来完成。

echo $class;
echo $teacher;
etc .... 

在查询之前的if语句内部。

我注意到$class = trim($_POST['calss']);它应该是&#39; class&#39 ;?还是卡尔斯?

答案 1 :(得分:1)

让我用你的代码解释你自己

$("button#btnUpdate").on('click', function (e){
        e.preventDefault();
        $("#inputStatus").val("update");
        $.ajax({
            type: 'post',
            url: 'changeClassStatus.php'
        });
    }

在服务器端,PHp期待来自ajax post请求的以下变量

$class  = trim($_POST['calss']);
  $teacher = trim($_POST['teacher']);
  $date   = $_POST['date'];
  $date   = $date."/01";
  $status = $_POST['status'];
  $empId = $_POST['empId'];

所以你的ajax请求应该在下面给出的语法中重新编写。

上述ajax请求不包含任何post参数。您应该向其添加数据属性。请参阅下面的示例

$.ajax({
    url: 'url',
    type: 'GET',
    data: { field1: "hello", field2 : "hello2"} ,
    contentType: 'application/json; 
    charset=utf-8'
});

希望这会有所帮助。感谢

答案 2 :(得分:1)

在jQuery ajax调用中,将字段变量以JSON格式传递给data属性,因为php脚本使用$ _POST获取字段变量值,使用type作为帖子。

$.ajax({
    url: 'changeClassStatus.php',
    type: 'POST',
    data: { calss: "hello", teacher : "hello2", date : "01/01/2017", status: "Active", empId : 12 },
    contentType: 'application/json; charset=UTF-8',
    success: function (response) {
        console.log(response.status);
    },
    error: function () {
        alert("error");
    }
}); 

在这里,如果在ajax调用中传入data属性,则php脚本将获取值。在if条件下,检查空变量并仅在值不为空时更新。

 <?php
      $class  = trim($_POST['calss']);
      $teacher = trim($_POST['teacher']);
      $date   = $_POST['date'];
      $date   = $date."/01";
      $status = $_POST['status'];
      $empId = $_POST['empId'];

if((!empty($status) && $status == 'update') && !empty($class) && !empty($teacher) && !empty($date) && !empty($empId)){
        $sqlUpdate = mysqli_query($conn,"UPDATE SchoolData SET Status='Pending' WHERE (Class=$class AND Teacher=$teacher AND ID='".$id[$i]."' AND YEAR(MonthYear)= YEAR('$date') AND MONTH(MonthYear)= MONTH('$date'));";
        $sqlExecute=$conn->query($sqlUpdate)or exit("Error code ({$conn->errno}): {$conn->error}");

        echo "Data Status Changed";
    }

    ?>