如何向PHP生成的表中添加更新按钮

如何向PHP生成的表中添加更新按钮,php,Php,在我的网站上,我使用php代码显示一个包含数据库信息的表。我正在尝试添加一个更新按钮,以便用户可以更新每一行的“状态”字段(换句话说,每一行的更新按钮,单击该按钮时,将仅更新该行的“状态”列)。我该怎么做?这是我的密码: <?php $link = mysqli_connect("localhost","root","", "correspondence"); if (!$link){ die("Can not connect: " . mysqli_error()); } my

在我的网站上,我使用php代码显示一个包含数据库信息的表。我正在尝试添加一个更新按钮,以便用户可以更新每一行的“状态”字段(换句话说,每一行的更新按钮,单击该按钮时,将仅更新该行的“状态”列)。我该怎么做?这是我的密码:

    <?php

$link = mysqli_connect("localhost","root","", "correspondence");
if (!$link){
die("Can not connect: " . mysqli_error());
}
mysqli_select_db($link, "correspondence");


$sql = "SELECT * FROM tracker WHERE department='N2'";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table>";
            echo "<tr>";  
                echo "<th>Tracking Number</th>";
                echo "<th>Subject</th>";
                echo "<th>Date Entered</th>";
                echo "<th>Originator</th>";
                echo "<th>Department</th>";
                echo "<th>Status</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
                echo "<tr>";
                echo "<td>" . $row['person_id'] . "</td>";
                echo "<td>" . $row['subject'] . "</td>";
                echo "<td>" . $row['date_entered'] . "</td>";
                echo "<td>" . $row['originator'] . "</td>";
                echo "<td>" . $row['department'] . "</td>";
                echo "<td>" . $row['status'] . "</td>";     
                echo "</form>";
                echo "</tr>";
        }
        echo "</table>";

        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

这将是最简单的解决方案

<?php

$link = mysqli_connect("localhost","root","", "correspondence");

if (!$link)
{
        die("Can not connect: " . mysqli_error());
}

mysqli_select_db($link, "correspondence");

$sql = "SELECT * FROM tracker WHERE department='N2'";


if($result = mysqli_query($link, $sql))
{

        if(mysqli_num_rows($result) > 0)
        {
                echo "<table>";
                echo "<tr>";  
                echo "<th>Tracking Number</th>";
                echo "<th>Subject</th>";
                echo "<th>Date Entered</th>";
                echo "<th>Originator</th>";
                echo "<th>Department</th>";
                echo "<th>Status</th>";
                echo "<th></th>";
                echo "</tr>";
                while($row = mysqli_fetch_array($result))
                {
                        echo "<tr>";
                        echo "<td>" . $row['person_id'] . "</td>";
                        echo "<td>" . $row['subject'] . "</td>";
                        echo "<td>" . $row['date_entered'] . "</td>";
                        echo "<td>" . $row['originator'] . "</td>";
                        echo "<td>" . $row['department'] . "</td>";
                        echo "<td>" . $row['status'] . "</td>";
                        echo "<td><form method=\"post\"><input type=\"submit\" name=\"$row['id']\">Update</input></form>";
                        echo "</tr>";
                }
        echo "</table>";

        mysqli_free_result($result);
        } 
                else
                {
                        echo "No records matching your query were found.";
                }
}
else
{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

mysqli_close($link);
?>

好的,有一种不使用js的方式来实现这一点,这一点都不优雅,如果您使用像jQuery这样的库来帮助发布请求,那么这是一种更干净的解决方案。我以后再给你看

首先,我们需要给每一行一个标识符。我们还提供了状态
td
a
class
,因此我们可以轻松地为它绑定一个click事件处理程序:

while($row = mysqli_fetch_assoc($result)){
  echo "<tr data-id='{$row['person_id']}'>";
  ...
  echo "<td class='status'>{$row['status']}</td>";
  echo "</tr>";
}

Ajax就可以了。状态应该切换还是有两个以上的可能状态?will update是指获取“状态”列的更新值还是设置它?@Majid Fouladpour,设置它。例如,用户在一行的“状态”列中键入“文档挂起”,然后单击“更新”按钮。刷新页面后,在同一个框中,现在显示“文档挂起”。然后,用户可以键入“完成”,然后单击更新。刷新后,该行的“状态”列现在显示为“完成”。在
tracker
表中,您有主键吗?如何唯一标识此表中的记录?收到错误(T_封装的_和_空格),预期标识符(T_字符串)或变量(T_变量)或数字(T_NUM_字符串)缺少分号。现在获取:解析错误:语法错误,意外“”(T_封装的_和_空格),预期标识符(T_字符串)或变量(T_变量)或者第100行C:\xampp\htdocs\correlations\N2.php中的数字(T_NUM_STRING)是:echo“Update”;
while($row = mysqli_fetch_assoc($result)){
  echo "<tr data-id='{$row['person_id']}'>";
  ...
  echo "<td class='status'>{$row['status']}</td>";
  echo "</tr>";
}
$(document).ready(function(){
  $("table").on("click", "td.status", function() {
    var $td = $(this),
        old = $td.text(),
        frm = '<input type="text" id="status" value="'+old+'/>' +
              '<button onclick="post_update(this);">Update</button>';
    $td.html(frm);
  });
});
function post_update(b) {
  var $tr   = $(b).parents('tr'),
      state = $tr.find("input").val(),
      id    = $tr.data("id");
  $.post("update_state.php", {s: state, r: id}, function(resp) {
    if("err" in resp) {
      alert(resp.err);
      return false;
     }
     $("tr[data-id='" + resp.id + "'] td.state").html(resp.state);
  }, "json");
}
<?php
require('dbconfig.php'); // Put DB constants in a single place
$link = mysqli_connect(DBHOST,DBUSER,DBPASS,DBNAME);
$stmt = mysqli_stmt_init($link);
$person_id = (int) $_POST['r'];
$new_state = $_POST['s'];
$query = "UPDATE `tracker` SET `state` = ? WHERE `person_id` = ? LIMIT 1";
mysqli_stmt_prepare($stmt, $query);
mysqli_stmt_bind_param($stmt, 'si', $new_state, $person_id);
mysqli_stmt_execute($stmt);
$n = mysqli_affected_rows($link);
if($n === 1) {
  $resp = ['id'=>$person_id, 'state'=>$new_state];
} else {
  $resp = ['err' => "No record updated. Either the person with id $person_id does not exist, or the original state was the same as the new one"];
}
header('Content-type: application/json; charset=utf-8');
die(json_encode($resp));
?>