【问题标题】:Upon clicking a link in a table row, change the cell value of the corresponding MYSQL database record?单击表行中的链接时,更改相应 MYSQL 数据库记录的单元格值?
【发布时间】:2014-10-08 16:59:50
【问题描述】:

我有一个 mysql 数据库,其字段结构如下:

id fname lname auxiliary calling releaseFrom user date notes status

我有一个对应于这些相同字段的表。在每一行的最后,我有一个链接,单击该链接时,应将相应行的“状态”字段更改为“已批准”。单击链接后,我很难找出如何指定要更改的记录。我假设我需要以某种方式定位正确行的 ID,但我不知道该怎么做。

This 是我拥有的表格的一个非常基本的设置。单击“已批准”链接时,它应该会更改 MYSQL 数据库记录。有谁知道如何完成我想做的事情?

【问题讨论】:

    标签: php jquery mysql sql


    【解决方案1】:

    第一个解决方案

    当您在服务器端构建表时,请确保有类似的东西

    <td><a href="your_script.php?action=approve&id=<?php echo RECORD_ID ?>">Archive</a></td>
    

    然后 HTML 将如下所示。如您所见,您将打印每条记录的此链接

    <!-- HTML --> 
    <tr>
        <td>43</td>
        <td>Jerry</td>
        <td>PFR</td>
        <td><a href="your_script.php?action=approve&id=111">Archive</a></td>
    </tr>
    

    虽然服务器端的代码应该是这样的

    //PHP [your_sccript.php]
    if(isset($_GET['action']) && $_GET['action'] == 'approve'){
        mysqli_query("
          UPDATE your_table
          SET status = 'approved'  // or use an integer, 1 in this case
          WHERE id = " . $_GET['id'] . "
       ");
       // if you use the second solution echo something here to the console, like
       echo "Post " . $_GET['id'] . " has been approved";
    }
    

    第二个解决方案

    如果您不想在每次点击Archive 链接后重新加载页面,请使用 Ajax。

    这是 PHP 在服务器端生成的表格行。您可能会注意到,JavaScript approve() 函数现在有两个参数;第二个是记录的id,而第一个是被点击元素的引用。

    <!-- HTML --> 
    <tr>
        <td>43</td>
        <td>Jerry</td>
        <td>PFR</td>
        <td><span onclick="approve(this, 111);">Archive</span></td>
    </tr>
    
    // JavaScript
    var approve = function(obj, id){
      var xhr = new XMLHttpRequest();
      var url = "your_script.php?action=approve&id=" + id;
      xhr.open("GET", url, true);
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
          // at this point we need to get the first parent TR to whom the SPAN belongs
          // if you want to replace only the TD (the parent of the SPAN)
          // change the TR to TD within the while loop below
          var tr = obj.parentNode;
          while(tr.nodeName != 'TR'){
            tr = tr.parentNode;
          }
          // as we have it, let's replace it with the response (new row) from the server
          tr.outerHTML = xhr.responseText;
        }
      };
      xhr.send();
    };
    
    // PHP
    if(isset($_GET['action']) && $_GET['action'] == 'approve'){
      // first, update the row
      mysqli_query("UPDATE table SET status = 1 WHERE id = " . $_GET["id"] . "");
      // and then select, and echo it back like this 
      $set = mysqli_query("SELECT * FROM table WHERE id = " . $_GET["id"] . "");
      $row = mysqli_fetch_array($set, MYSQLI_ASSOC);
      echo '<TR>' . 'ALL_THE_TD' . '</TR>';
      // so, we echo the same row, but the updated one
      // this will be used by JavaScript function to replace the old TR 
    }
    

    【讨论】:

    • 当然,$_GET['id'] 应该在使用前正确转义和/或验证。由于您使用的是mysqli_* 调用,因此您应该使用preparebind_params 而不是query
    • @jcaron 感谢您抓住那个缺失的报价 ;) 对于mysqli,我只是想让他知道他应该使用它,我希望他也会使用其他相关的东西 :)
    • @BrianLender 很高兴我能帮上忙 ;)
    • @hex494D49 我尝试使用您给我的 AJAX 解决方案,效果很好,只是页面没有更新。我单击该链接,似乎在我手动刷新页面之前没有任何反应,然后它显示更新确实已完成。你知道我可能错过了什么吗?
    • @BrianLender 检查更新的第二个解决方案,如果您需要任何进一步的帮助,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 2022-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多