javascript - 无法在网站上编辑表格中的数据库条目
问题描述
我正在开发一个网站数据库系统,我能够编辑内联表格中列出的每个内容。我只需单击它,键入以添加新数据,然后单击它会将新数据保存到数据库中。我可以单击每个条目并开始输入,但它不会保存更新的信息。我觉得它有一个简单的解决方案,但我一直无法找到它。我已经查看了一段时间,似乎无法弄清楚。任何帮助深表感谢..
索引.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";
}
}
?>
这是我收到的错误消息
错误图片
解决方案
推荐阅读
- javascript - Angular Material:“在'./material/material.module'中找不到导出'MaterialComponents'
- sql - 找到至少两行对应于具有不同值的相同 id
- javascript - 更改数据表中特定列的颜色
- python - 将 Selenium 与 Chrome 一起使用不会加载网页
- python - 使用 Python 处理 ctypes 函数中的无限循环
- java - raspbian 上的 flyway 问题:无法执行二进制文件:执行格式错误
- ansible - 使用 Ansible 变量填充 Jinja2 模板?
- apache - 如何在 AEM 中向公众隐藏内容/站点结构?
- java - 身份证查char方法?
- loops - 迭代器是否返回对项目的引用或 Rust 中项目的值?