首页 > 解决方案 > JQuery-Tabledit 一次只更新一列(我的代码有什么问题)

问题描述

我正在使用 JQuery-Tabledit 来更新 MySQL 表。

我面临的问题是,当我更新第一列的单元格值时,第二列的相同行值消失了。

我无法弄清楚我在这里做错了什么。

这是我的 JQuery-Tabledit 代码

$('#example1').Tabledit({
url: 'logic-edit-delete.php',
editButton: false,
deleteButton: false,
columns: {
    identifier: [0, 'id'],
    editable: [[4, 'Status', '{"": "Select","P": "Present", "PL": "Planed Leave", "UPL": "UnPlaned Leave", "WO": "Week Off", "COff": "Comp Off", "PH": "Public Holiday", "UPHD": "UnPlaned Half Day", "PHD": "Planed Half Day", "LWP": "Leave Without Pay"}'], [5, 'Leavetype', '{"": "Select","SL": "Sick Leave", "CL": "Casual Leave"}']]

},

和 PHP 代码来更新我的 SQL 表

<?php

include('db-connect.php');

$input = filter_input_array(INPUT_POST);

if ($input['action'] === 'edit') 
{   
    $sql = "UPDATE attendance SET Status ='" . $input['Status'] . "', Leavetype ='" . $input['Leavetype'] . "'" ." WHERE id='" . $input['id'] . "'";

    mysqli_query($con,$sql);
} 
if ($input['action'] === 'delete') 
{
    mysqli_query($con,"DELETE FROM attendance   WHERE id='" . $input['id'] . "'");
} 


mysqli_close($mysqli);

echo json_encode($input);
?>

您的参考图像的屏幕截图 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

标签: phpjquerymysql

解决方案


发生这种情况是因为您每次都尝试更新两列。您只需要根据以下条件进行更新:

    if(isset($input['Status']) && !empty($input['Status'])) {
        $sql = "UPDATE attendance SET Status ='" . $input['Status'] . "' WHERE id='" . $input['id'] . "'";   
    } else if(isset($input['Leavetype']) && !empty($input['Leavetype'])) {
        $sql = "UPDATE attendance SET Leavetype ='" . $input['Leavetype'] . "' WHERE id='" . $input['id'] . "'";
    }

推荐阅读