id car make sales
1 panamera porsche 100
2 italia ferrari 200
3 volante astonmartin 300
4 avantador lamborghini 400
5 slk mercedes 500
伙计们,我的数据库中有这个简单的表格。我将在一段时间循环中呼应这张表。
<ul>
<?php
$query = "SELECT * FROM inplace LIMIT 0, 6";
$result = mysql_query($query) or die ('Query couldn't be executed');
while ($row = mysql_fetch_assoc($result)) {
echo '<li class="editable" data-id="'.$row['id'].'" data-col="car"><a href="#">'.$row['car'].'</a></li>';
echo '<li class="editable" data-id="'.$row['id'].'" data-col="make"><a href="#">'.$row['make'].'</a></li>';
echo '<li class="editable" data-id="'.$row['id'].'" data-col="sales"><a href="#">'.$row['sales'].'</a></li>';
}
?>
</ul>
这个想法是使用 jQuery 就地编辑器更新此表。所以这是代码-
$(document).ready(function()
{
$(".editable").bind("dblclick", replaceHTML);
$(".editable2").bind("dblclick", replaceHTML2);
$(".btnSave, .btnDiscard").live("click", handler);
function handler()
{
if ($(this).hasClass("btnSave"))
{
var str = $(this).siblings("form").serialize();
$.ajax({
type: "POST",
async: false,
url: "handler.php",
data: str,
});
}
}
function replaceHTML()
{
var rowId = $(this).parent('li').data('id');
var colName = $(this).parent('li').data('col');
var buff = $(this).html()
.replace(/"/g, """);
$(this).addClass("noPad")
.html("<form><input type="text" name="" + colName + "" value="" + buff + "" /> <input type="text" name="buffer" value="" + buff + "" /><input type="text" name="id" value="" + rowId + "" /></form><a href="#" class="btnSave">Save changes</a> <a href="#" class="btnDiscard">Discard changes</a>")
.unbind('dblclick', replaceHTML);
}
}
);
这是我从互联网上获得的就地编辑代码,我只是为了理解代码而将其撕成基本级别。 Behrang Saeedzadeh帮助我即兴创作了"替换HTML"功能。
这是处理程序.php文件中的更新查询-
<?php
require("db.php");
if (isset($_POST['id']) && isset($_POST['car'])) {
$id = mysql_real_escape_string($_POST['id']);
$car = mysql_real_escape_string($_POST['car']);
$query = "UPDATE inplace SET car ='$car' WHERE id='$id'";
$result = mysql_query($query) or die ('Query couldn't be executed');
if ($result) {echo 1;}
}
else if (isset($_POST['id']) && isset($_POST['make'])) {
$id = mysql_real_escape_string($_POST['id']);
$make = mysql_real_escape_string($_POST['make']);
$query = "UPDATE inplace SET make ='$make' WHERE id='$id'";
$result = mysql_query($query) or die ('Query couldn't be executed');
if ($result) {echo 1;}
}
else if (isset($_POST['id']) && isset($_POST['sales'])) {
$id = mysql_real_escape_string($_POST['id']);
$sales = mysql_real_escape_string($_POST['sales']);
$query = "UPDATE inplace SET sales ='$sales' WHERE id='$id'";
$result = mysql_query($query) or die ('Query couldn't be executed');
if ($result) {echo 1;}
}
?>
在更新查询中,我必须为每列编写不同的查询。问题是如何仅对所有列使用一个查询进行更新?
if(isset($_POST['id']) {
$id = mysql_real_escape_string($_POST['id']);
$arr_check = array("car", "make", "sales");
$result = array();
foreach($arr_check as $check) {
if(isset($_POST[$check]))
$result[] = $check . '="' . mysql_real_escape_string($_POST[$check]) . '"';
}
$result = implode(", ", result);
if($result != '') {
$query = "UPDATE inplace SET {$result} WHERE id='{$id}'";
$result = mysql_query($query) or die ('Query couldn't be executed');
if ($result) echo 1;
}
}
这应该或多或少地做到
首先,最好将整个列表设置为表单,并将现有记录存储在隐藏的表单字段中。然后,您应该让处理程序.php脚本检查是否提交了任何新的表单条目,并将它们存储到变量中。如果未创建新条目,则变量将包含默认值。
然后,您可以一次性提交整个更新查询:
$query = "UPDATE inplace SET car ='$car', make='$make', sales='$sales' WHERE id='$id'";
$result = mysql_query($query) or die ('Query couldn't be executed');
if ($result) echo 1;