简体   繁体   English

仅使用一个查询来更新数据库中的列-就地编辑

[英]using only one query to update columns in the database - inplace edit

id  car         make          sales
1  panamera    porsche       100 
2  italia      ferrari       200
3  volante     astonmartin   300
4  avantador   lamborghini   400
5  slk         mercedes      500

So guys, i have this simple table in my database. 伙计们,我的数据库中有这个简单的表。 And i'm gonna echo this table in a while loop. 我将在while循环中回显此表。

<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>

The idea is to update this table using jQuery in-place editor. 想法是使用jQuery就地编辑器更新此表。 So here is the code- 所以这是代码-

$(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, "&quot;");
    $(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);   


    }

}
); 

This is an in-place edit code i got it from the internet and i just tore it down to basic level just to understand the codes. 这是一个就地编辑代码,我从互联网上获得了它,我只是为了理解代码而将其撕毁到基本级别。 Behrang Saeedzadeh helped me improvise "replace HTML" function. Behrang Saeedzadeh帮助我即兴创作了“替换HTML”功能。

And here is the update query in handler.php file - 这是handler.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;}
 } 

?> 

Here in the update query, i have to write a different query for each column. 在更新查询中,我必须为每一列编写一个不同的查询。 The question is how do i update using only one query for all the columns? 问题是如何仅对所有列使用一个查询进行更新?

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; 

   }

}

that should more or less do it 应该或多或少地做到这一点

First of all, you might be better off making the entire list a form to begin with, and storing the existing records in hidden form fields. 首先,最好将整个列表作为一个表单,然后将现有记录存储在隐藏的表单字段中。 Then, you should have the handler.php script check if any new form entries were submitted, and store them into variables. 然后,应该让handler.php脚本检查是否提交了任何新的表单条目,并将其存储到变量中。 If no new entry was made, the variables will contain the default value. 如果未进行任何输入,则变量将包含默认值。

Then, you can submit the whole update query in one shot: 然后,您可以一次提交整个更新查询:

$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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM