使用php和jquery编辑表
我试图用php和ajax修改表中单元格的内容。 页面中的内容已更改,但未在数据库中注册的问题。 这是index.php页面:使用php和jquery编辑表,php,ajax,Php,Ajax,我试图用php和ajax修改表中单元格的内容。 页面中的内容已更改,但未在数据库中注册的问题。 这是index.php页面: <?php include 'connexion.php'; $sql = 'SELECT * FROM liste_user_tbl'; $result = mysql_query($sql) or die(__LINE__.mysql_error().$sql); ?> <!DOCTYPE html PUBLIC "-//W3C//
<?php
include 'connexion.php';
$sql = 'SELECT * FROM liste_user_tbl';
$result = mysql_query($sql) or die(__LINE__.mysql_error().$sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Modification "inline" de données</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script>
$(function(){
var message_status = $("#status");
$("td[contenteditable=true]").blur(function(){
var field_userid = $(this).attr("id") ;
var value = $(this).text() ;
$.post('test1.php' , field_userid + "=" + value, function(data){
if(data != '')
{
message_status.show();
message_status.text(data);
setTimeout(function(){message_status.hide()},3000);
}
});
});
</script>
</head>
<body>
<h1>Utilisateurs</h1>
<table id="table-utilisateurs">
<tr>
<th>Nom</th>
<th>Prénom</th>
</tr>
<?php
while($user = mysql_fetch_assoc($result))
{
?>
<tr>
<td id="<?php echo $user['id']; ?>" contenteditable="true">
<?php echo $user['nom']; ?>
</td>
<td id="<?php echo $user['id']; ?>" contenteditable="true">
<?php echo $user['prenom']; ?>
</td>
</tr>
<?php
}
?>
</table>
</body>
</html>
<?php
mysql_close();
?>
修改“内联”de données
$(函数(){
var消息_状态=$(“#状态”);
$(“td[contentediate=true]”)。模糊(函数(){
var field_userid=$(this.attr(“id”);
var值=$(this.text();
$.post('test1.php',字段\用户ID+“=”+值,函数(数据){
如果(数据!='')
{
消息_status.show();
消息状态文本(数据);
setTimeout(函数(){message_status.hide()},3000);
}
});
});
利用者
笔名
名词
这是test1.php:
<?php
if(!empty($_POST))
{
include "connexion.php";
foreach($_POST as $field_name => $val)
{
//clean post values
$field_userid = strip_tags(trim($field_name));
$val = strip_tags(trim(mysql_real_escape_string($val)));
//from the fieldname:user_id we need to get user_id
$split_data = explode(':', $field_userid);
$user_id = $split_data[1];
$field_name = $split_data[0];
if(!empty($user_id) && !empty($field_name) && !empty($val))
{
//update the values
mysql_query("UPDATE liste_user_tbl SET $field_name = '$val' WHERE id = $user_id") or mysql_error();
echo "Updated";
} else {
echo "Invalid Requests";
}
}
} else {
echo "Invalid Requests";
}
?>
首先,$field\u name
未在代码中定义,这就是如果启用调试,查询将产生错误的原因
其次,您的代码非常容易受到SQL注入的攻击。您使用的是用户发布的数据,没有任何过滤,这可能会导致丢失整个数据库数据。
第三,您仍然在使用过程php和“old School”数据库连接。相反,您可以使用和
打印(邮政美元)
接收post数据并显示
检查post数据是否有问题检查您在浏览器控制台中传递到test1.php的数据。转储您的post数据。$field\u name
定义在这一行foreach($\u post as$field\u name=>$val)
。我认为您在最后一句话中的意思是OOP(不是相反)。