简体   繁体   中英

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.

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

And here is the update query in handler.php file -

<?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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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