gpt4 book ai didi

php - 将表条目实时更新到数据库 SQL PDO AJAX

转载 作者:行者123 更新时间:2023-11-29 11:29:41 25 4
gpt4 key购买 nike

我尝试在主页上制作一个可编辑表格,在您离开“字段”或按 Enter 键后自动保存新条目,但有些东西不起作用。一切都很好,直到我想将新条目保存到数据库中。我触发了 PDO-SQL-Statment 并且它有效。

对我来说,table_edit_ajax.php 似乎不起作用,但我不知道为什么!

希望你们能帮助我吗?

我使用的是普通的 mysql 数据库

主页代码:

<Table>

$getIDBusinessRessources = $dbPDO->geteditableSingleBusinessRessoure();


foreach($getIDBusinessRessources as $getidBusiness){
$id = $getidBusiness['checkid'] ;


echo '<tr id="'
. $getidBusiness['checkid']
. '" '
. 'class="edit_tr"'
. '>';

// Spalte Ressourcenname
echo '<td class="edit_td">'
.'<span id="RessourceName_'
.$getidBusiness['checkid']
. '" '
. 'class="text">'
.$getidBusiness['Rn']
.'</span>'
.'<input type="text" value="'
.$getidBusiness['Rn']
. '" class="editbox" id="Ressourcname_Input_'
. $getidBusiness['checkid']
.'">'
.'</td>';

// Spalte Amount
echo '<td class="edit_td">'
.'<span id="Amount_'
.$getidBusiness['checkid']
. '" '
. 'class="text">'
.$getidBusiness['AM']
.'</span>'
.'<input type="text" value="'
.$getidBusiness['AM']
. '" class="editbox" id="Amount_Input_'
. $getidBusiness['checkid']
.'" '
.'</td>';
</tr>
</table>

JS:

$( document ).ready(function() {

$(".edit_tr").click(function()
{
var ID=$(this).attr('id');
$("#RessourceName_"+ID).hide();
$("#Amount_"+ID).hide();
$("#Ressourcname_Input_"+ID).show();
$("#Amount_Input_"+ID).show();
}).change(function()
{
var ID=$(this).attr('id');
var first=$("#Ressourcname_Input_"+ID).val();
var last=$("#Amount_Input_"+ID).val();
var dataString = 'id='+ ID +'&RessourceName='+first+'&Amount='+last;
$("#RessourceName_"+ID).html('<img src="img/bearbeiten.png" />'); // Loading image

if(first.length>0&& last.length>0)
{

$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
cache: false,
success: function(html)
{
$("#RessourceName_"+ID).html(first);
$("#Amount_"+ID).html(last);
}
});
}
else
{
alert('Enter something.');
}

});

// Edit input box click action
$(".editbox").mouseup(function()
{
return false
});

// Outside click action
$(document).mouseup(function()
{
$(".editbox").hide();
$(".text").show();
});

Ajax 代码:

<?php

include "DBconnection.php";

if($_POST['id']){

$id = $_POST['checkid'];
$RessourceName = $_POST['Rn'];
$Amount = $_POST['AM'];
$dbPDO->UpdateLiveTableEntries($id,$RessourceName, $Amount );


}
?>

至少还有数据库更新代码

function UpdateLiveTableEntries($id ,$Ressourcename, $Amount ){

// echo '<script type="text/javascript"> alert("inside");</script>';
$stmt = self::$_db->prepare("UPDATE BalancesheetInput SET RessourceName =:ressname , Amount =:menge WHERE ID =:id");
$stmt->bindParam(":id", $id);
$stmt->bindParam(":ressname", $Ressourcename);
$stmt->bindParam(":menge", $Amount);

$stmt->execute();




}

最佳答案

如果您要发送此内容:

var dataString = 'id='+ ID +'&RessourceName='+first+'&Amount='+last;
^^ ^^^^^^^^^^^^^ ^^^^^^

你为什么要找这些?

$id                 = $_POST['checkid'];
^^^^^---you aren't sending a 'checkid'
$RessourceName = $_POST['Rn'];
^-- you aren't sending an 'Rn'
$Amount = $_POST['AM'];
^^-- you aren't sending an 'AM'

关于php - 将表条目实时更新到数据库 SQL PDO AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37621627/

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