首页 > 解决方案 > 无法在网站上编辑表格中的数据库条目

问题描述

我正在开发一个网站数据库系统,我能够编辑内联表格中列出的每个内容。我只需单击它,键入以添加新数据,然后单击它会将新数据保存到数据库中。我可以单击每个条目并开始输入,但它不会保存更新的信息。我觉得它有一个简单的解决方案,但我一直无法找到它。我已经查看了一段时间,似乎无法弄清楚。任何帮助深表感谢..

索引.php

<?php
include 'db.inc.php';

$sql= "SELECT * FROM crosswalk";
$query = $db->prepare($sql);
$query->execute();

?>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    function activate (element) {
  //      alert('clicked')
    }
    function updateValue(element, column, id){
        var value = element.innerText
        $.ajax({
            url:'backend.php',
            type: 'post',
            data:{
                value: value,
                column: column,
                id: id
            },
            success:function(php_result){
                console.log(php_result);
            }
        })
    }
</script>
<table>
    <th>Category</th>
    <th>Product Category</th>
    <th>NIGP</th>
    <th>NIGP Short Description</th>
    <th>NIGP Full Description</th>
    <th>P-Group</th>
    <th>Category Specialist</th>
    <th>GL Unit Price < $500</th>
    <th>GL Unit Price < $500 - $4,999</th>
    <th>GL Unit Price - $5000 +</th>
<?php
while($row = $query->fetch()){
    $c = $row['Category'];
    $p = $row['Product Category'];
    $n = $row['NIGP'];
    $s = $row['NIGP Short Description'];
    $f = $row['NIGP Full Description'];
    $g = $row['P-Group'];
    $gl1 = $row['GL Unit Price<$500'];
    $gl2 = $row['GL Unit Price<$500-$4,999'];
    $gl3 = $row['GL Unit Price-$5000+'];
    $id = md5($row['id']);

 ?>
 
<tr>
<td><div contenteditable="true" onBlur="updateValue(this, 'Category','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $c; ?></div></td>    
<td><div contenteditable="true" onBlur="updateValue(this, 'Product Category','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $p; ?></div></td> 
<td><div contenteditable="true" onBlur="updateValue(this, 'NIGP','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $n; ?></div></td> 
<td><div contenteditable="true" onBlur="updateValue(this, 'NIGP Short Description','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $s; ?></div></td> 
<td><div contenteditable="true" onBlur="updateValue(this, 'NIGP Full Description','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $f; ?></div></td> 
<td><div contenteditable="true" onBlur="updateValue(this, 'P-Group','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $g; ?></div></td> 
<td><div contenteditable="true" onBlur="updateValue(this, 'GL Unit Price<$500','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $gl1; ?></div></td> 
<td><div contenteditable="true" onBlur="updateValue(this, 'GL Unit Price<$500-$4,999','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $gl2; ?></div></td> 
<td><div contenteditable="true" onBlur="updateValue(this, 'GL Unit Price-$5000+','<?php echo $id; ?>')" onClick="activate(this)"><?php echo $gl3; ?></div></td> 

</tr> 
<?php    
   
}
?>
       
</table>

后端.php

<?php
include 'db.inc.php';

if(isset($_POST['id'])){
    $value = $_POST['value'];
    $column = $_POST['column'];
    $id = $_POST['id']; //md5
    
   $sql="UPDATE crosswalk SET $column = :value WHERE md5(id) = :id LIMIT 1";
   $query = $db->prepare($sql);
   $query->bindParam('value',$value);
   $query->bindParam('id',$id);
   
   if($query->execute()){
       echo "Update Successful";
       
   }else{
       echo "Failure";
   }
}

?>

这是我收到的错误消息

在此处输入图像描述

错误图片

在此处输入图像描述 在此处输入图像描述

标签: javascriptphphtmlajaxdatabase

解决方案


推荐阅读