简体   繁体   English

可编辑的数据表PHP MySQL jQuery

[英]Editable Datatables PHP MySQL jQuery

I have a table which is fed dynamically from the database, this works as it should, I've then put the datatables functionality on the top which is working well, the sorting, search, the pagination is again working as it should. 我有一个从数据库动态提供的表,这可以正常工作,然后我将数据表功能放在顶部,运行良好,排序,搜索,分页再次正常工作。

I'm now looking at being able to edit a row and update the data back into the db and refresh the table. 我现在正在寻找能够编辑行并将数据更新回数据库并刷新表。

My table structure 我的桌子结构

<table class="table table-striped table-hover table-bordered" id="tobebookedtable">
<thead>
    <tr>
        <th style="display:none;">PropID</th>
        <th>ProjectNo</th>
        <th>Householder</th>
        <th>HouseNoName</th>
        <th>Street Name</th>
        <th>Postcode</th>
        <th>Telephone</th>
        <th>EPCPlanned</th>
        <th>TAM</th>
    </tr>
</thead>
<tbody>
    <tr>
        <?php
            $planning = db::getInstance()->query('CALL sp_tobebooked()');
            foreach ($planning->results() as $planning) {
        ?>
        <td style="display:none;"><?php echo $planning->PropID; ?></td>
        <td><?php echo $planning->ProjectNo; ?></td>
        <td><?php echo $planning->Householder; ?></td>
        <td><?php echo $planning->HouseNoName; ?></td>
        <td><?php echo $planning->StreetName; ?></td>
        <td><?php echo $planning->Postcode; ?></td>
        <td><?php echo $planning->Telephone; ?></td>
        <td><?php echo $planning->EPCPlanned; ?></td>
        <td><?php echo $planning->TAM; ?></td>
    </tr>
    <?php }; ?>
</tbody>
</table>

The EPCPlanned is the only field I want to update. EPCPlanned是我想要更新的唯一字段。

This here is my code for the jQuery side, the edit ability works, I can click into a cell and edit the record. 这是我的jQuery方面的代码,编辑能力有效,我可以点击进入单元格并编辑记录。

<script type="text/javascript">
$(document).ready(function() {
 $('#tobebookedtable').dataTable( {
        //"bProcessing": true,
        //"bServerSide": true,
        //"bJQueryUI": true,
        "bPaginate": true,
        "bStateSave": true
    } );

    /* Init DataTables */
    var oTable = $('#tobebookedtable').dataTable();

    /* Apply the jEditable handlers to the table */
    oTable.$('td').editable( 'tobebooked_edit.php', {
        "callback": function( sValue, y ) {
            var aPos = oTable.fnGetPosition( this );
            oTable.fnUpdate( sValue, aPos[0], aPos[1] );
            //window.location.reload();
        },
        "submitdata": function ( value, settings ) {
            console.log(value);
            console.log(settings);
            return {
                "row_id": this.parentNode.getAttribute('id'),
                "column": oTable.fnGetPosition( this )[2]
            };
        },
        "height": "26px",
        "width": "100%"
    } );    
   } );
  </script>

this is the tobebooked_edit.php 这是tobebooked_edit.php

<?php 
require 'core/init.php';

    $table="temp_tobebooked";
$rawdata    = $_POST;
$query      = "show columns from $table";
$result= mysql_query($query) or die(mysql_error());

$fields     = array();
while ( $row = mysql_fetch_assoc($result) )
{
            $fieldname = $row['Field'];
            array_push($fields, $fieldname);
}

$id             = $rawdata['row_id'];
$value          = $rawdata['value'];
$column         = $rawdata['column'];
$column         = $column + 1;
$fieldname      = $fields[$column];
$value = utf8_decode($value);

$query  = "update $table set $fieldname = '$value' where PropID = '$id'";
$result = mysql_query($query);

if (!$result) { echo "Update failed"; }
else          { echo "UPD: $value"; }
?>

When it all runs and I update a record the field Updates on the screen and shows with the "UPD:" and the entered value. 当它全部运行并且我更新记录时屏幕上的字段更新并显示“UPD:”和输入的值。

But when I check my database there is no record updated. 但是当我检查我的数据库时,没有更新记录。 No errors show either 没有任何错误显示

How can I get the data to update in my db? 如何在数据库中更新数据?

First of all, I think that this 首先,我认为这一点

<tr>
    <?php
        $planning = db::getInstance()->query('CALL sp_tobebooked()');
        foreach ($planning->results() as $planning) {
    ?>

should be 应该

<?php
        $planning = db::getInstance()->query('CALL sp_tobebooked()');
        foreach ($planning->results() as $planning) {
    ?>

<tr>

Then, with 然后,用

"row_id": this.parentNode.getAttribute('id'),

for each td element, you select the tr element and search for the id attribute, that doesn't exists 对于每个td元素,选择tr元素并搜索不存在的id属性

Firstly as per rsella's comment you nee to move the row inside the loop. 首先根据rsella的评论你需要在循环中移动行。 Secondly as suspected you are holding the ID in a hidden cell which is infact a sibling of the editable elements rather than the parent. 其次,怀疑你是在一个隐藏的单元格中持有ID,这实际上是可编辑元素的兄弟,而不是父元素。 Try changing the table structure to the following 尝试将表结构更改为以下内容

<tbody>
    <?php
    $planning = db::getInstance()->query('CALL sp_tobebooked()');
    foreach ($planning->results() as $planning) {
    ?>
    <tr id="<?php echo $planning->PropID; ?>" >
        <td><?php echo $planning->ProjectNo; ?></td>
        <td><?php echo $planning->Householder; ?></td>
        <td><?php echo $planning->HouseNoName; ?></td>
        <td><?php echo $planning->StreetName; ?></td>
        <td><?php echo $planning->Postcode; ?></td>
        <td><?php echo $planning->Telephone; ?></td>
        <td><?php echo $planning->EPCPlanned; ?></td>
        <td><?php echo $planning->TAM; ?></td>
    </tr>
    <?php };
    ?>
</tbody>

This should mean that the parent has an ID and thus this.parentNode.getAttribute('id') should be able returning the required value. 这应该意味着父有一个ID,因此this.parentNode.getAttribute('id')应该能够返回所需的值。

As you're only editing the EPCPlanned cell then an alternative would be 由于您只编辑EPCPlanned单元格,因此可以选择其他方法

<tbody>  
    <?php
    $planning = db::getInstance()->query('CALL sp_tobebooked()');
    foreach ($planning->results() as $planning) {
    ?>
    <tr>
        <td><?php echo $planning->ProjectNo; ?></td>
        <td><?php echo $planning->Householder; ?></td>
        <td><?php echo $planning->HouseNoName; ?></td>
        <td><?php echo $planning->StreetName; ?></td>
        <td><?php echo $planning->Postcode; ?></td>
        <td><?php echo $planning->Telephone; ?></td>
        <td id="<?php echo $planning->PropID; ?>"><?php echo $planning->EPCPlanned; ?></td>
        <td><?php echo $planning->TAM; ?></td>
    </tr>
    <?php };
    ?>
</tbody>

and in your JQuery change "row_id": this.parentNode.getAttribute('id') to this.getAttribute('id') 并在你的JQuery中更改"row_id": this.parentNode.getAttribute('id')this.getAttribute('id')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM