简体   繁体   English

一次更新1行,当对同一用户使用x-editable时,每行具有不同的值

[英]Update 1 row at a time and each row has a different value when using x-editable for the same user

I have a HTML table with, for this example, 2 rows (could be more). 对于此示例,我有一个HTML表,其中包含2行(可能更多)。 The middle column is editable and each row in the HTML table is a different column in the database. 中间列是可编辑的,HTML表中的每一行都是数据库中的不同列。 When I edit row 1 and refresh the page row 2 now has the same value as row 1. I do not want this. 当我编辑第1行并刷新页面时,第2行现在与第1行具有相同的值。我不希望这样做。 The user id (pk) will be the same for each row. 每行的用户ID(pk)相同。 The documentation about creating a new record says this would work with updating existing, but does not really help me. 有关创建新记录的文档说,这可以与更新现有记录一起使用,但对我没有帮助。 I am looking for as much help as I can get. 我正在寻找尽可能多的帮助。

Here is an image of my table: 这是我桌子的图片: 在此处输入图片说明

How do I get each row to keep its value and not update all other rows? 如何获取每一行以保持其值而不更新其他所有行?

For example if I update the column in row 1, I want only row 1's column to be updated. 例如,如果我更新第1行中的列,则只希望更新第1行的列。

Please help! 请帮忙!

Here is my code: 这是我的代码:

HTML Code: HTML代码:

<td><a class="myeditable" data-name="fb_url"  data-type="text" data-pk="<?php echo ($userid);?>" title="Edit"><?php echo ($result['fb_url']);?></a></td>                    
<td><a class="myeditable" data-name="tw_url" data-type="text" data-pk="<?php echo ($userid);?>" title="Edit"><?php echo ($result['tw_url']);?></a></td>

Post.php Code Post.php代码

require("config.php");

$pk = $_POST['pk'];
$name = $_POST['name'];
$value = $_POST['value'];

    if(!empty($value)) {
            try // save user selection to the database
              {
                $stmt = $db->prepare("UPDATE social_preferences SET fb_url = :fburl, tw_url = :twurl WHERE user_id = :userID AND ");
                $stmt->bindParam(":userID", $pk, PDO::PARAM_INT);
                $stmt->bindParam(':twurl', $value);
                $stmt->bindParam(':fburl', $value);
                $stmt->execute();
               }  catch(PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
    }else {
        echo 'No value...';
    }

JS Code: JS代码:

$(document).ready(function() {
//toggle `popup` / `inline` mode
$.fn.editable.defaults.mode = 'inline';       


    $('.myeditable').editable({
            url: 'post.php',
            ajaxOptions: {
                type: 'post',
            },
            success: function(data, config) {
                if(data && data.id) {
                     //set pk
                    $(this).editable('option', 'pk', data.id);                 
                }
            }
    });
});

When I check the network tab in Chrome dev tools I see this: Method: POST Status: 302 Moved Temporarily 当我在Chrome开发工具中检查“网络”标签时,看到以下内容:方法:POST状态:302临时移动

OP is talking about rows, but in fact it are columns. OP在谈论行,但实际上它是列。

$accepted_names = array('fb_url', 'tw_url');
if (in_array($_POST['name'], $accepted_names)) {
    if(!empty($_POST['value'])) {
        $value = $_POST['value'];
        $id = $_POST['pk'];
        try {
            $stmt = $db->prepare('UPDATE social_preferences SET ' . $_POST['name'] . ' = :value
                                  WHERE user_id = :id');
            $stmt->bindParam(':id', $id, PDO::PARAM_INT);
            $stmt->bindParam(':value',$value);
            $stmt->execute();
        }
        catch (PDOExeption $e) {
            echo json_encode(array('status' => 'failed', 'msg' => $e->getMessage()));
            exit;
        }
        echo json_encode(array('id' => $id, 'status' => 'oké', 'msg' => $value));
    }
    else {
         echo jsonencode(array('status' => 'failed', 'msg' => 'No value ....'));
    }
}
else {
    header($_SERVER['SERVER_PROTOCOL'] . ' 422 Unprocessable entity');
}

I assume that you use oracle SQL. 我假设您使用oracle SQL。

Put all the users you want to update in an array and use foreach to iterate over them. 将要更新的所有用户放入一个数组中,并使用foreach对其进行迭代。

Execute your update statement for every one. 对每个执行更新语句。

I don't know if it is possible to run multiple updates in one statement, but I don't think so. 我不知道是否可以在一个语句中运行多个更新,但是我不这样认为。

EDIT:To be sure that only one row is affected use unique identifiers such as user ids if possible. 编辑:为确保仅影响一行,请尽可能使用唯一标识符,例如用户ID。

in fact , you just need to follow the documentation of the X - table in here X-Table 实际上,您只需要遵循此处X表的X表文档

Ok, this is a information tag. 好的,这是一个信息标签。 data-name="your field column in database" data-pk="your primary key" in data pk you can input multiple PK. 您可以在数据库中输入data-name =“您在数据库中的字段列” data-pk =“您的主键”,然后输入多个PK。

Now please try this example. 现在,请尝试以下示例。

HTML: HTML:

<td>
   <a class="myeditable" data-name="fb_url"  data-type="text" data-pk="<?php echo $userid;?>" data-url="post.php" title="Edit"><?php echo ($result['fb_url']);?></a>
</td>      

JS Code: JS代码:

<script>  
        $(document).ready(function() {
            $('.myeditable').editable();
        });
    </script>

Post.php Code Post.php代码

if(!empty($_POST['name'])) {
 $pk = $_POST['pk']; // post from data-pk
     $name = $_POST['name']; // post from data-name
     $value = $_POST['value']; // this is your value
            $stmt=$db->prepare("UPDATE social_preferences SET $name= '".$value."' WHERE your_id= '".$pk."'");
            $stmt->execute();
}else {
    header($_SERVER['SERVER_PROTOCOL'] . ' 422 Unprocessable entity');
}

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

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