gpt4 book ai didi

php - 可编辑数据表 PHP MySQL jQuery

转载 作者:行者123 更新时间:2023-12-01 07:56:17 26 4
gpt4 key购买 nike

我有一个从数据库动态输入的表,它应该正常工作,然后我将数据表功能放在顶部,该功能运行良好,排序、搜索、分页再次正常工作.

我现在正在考虑能够编辑一行并将数据更新回数据库并刷新表。

我的表结构

<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>

EPCPlanned 是我想要更新的唯一字段。

这是我的 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>

这是 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"; }
?>

当一切运行并且我更新记录时,屏幕上的字段更新并显示“UPD:”和输入的值。

但是当我检查数据库时没有更新记录。也没有显示错误

如何获取要在数据库中更新的数据?

最佳答案

首先,我认为这个

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

应该是

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

<tr>

然后,与

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

对于每个 td 元素,您选择 tr 元素并搜索不存在的 id 属性

关于php - 可编辑数据表 PHP MySQL jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24091492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com