PHP - 如何编辑已显示在 HTML 表格中的 PHP 表格的内容?

标签 php html mysql

<分区>

我正在为学校类(class)做一个计算项目。它涉及 PHP、MySQL 和 HTML - 本质上是构建基于 Web 的应用程序。

我在名为“cp11641_users”的数据库中有一个名为“users”的 PHP 表 此表的内容已使用以下 mysql_query 回显到 html 表中:

<div id='accordion'>   <ul>
            <li>
              <a href='#one'><h4>Edit User Table</h4></a>
              <div id='one' class='accordion'>";
                $query = "SELECT * FROM users"; //You don't need a ; like you do in SQL 
                $result = mysql_query($query);

        echo "<table class='container'>"; // start a table tag in the HTML 
        echo " <thead>      <tr>
            <th>Firstname</th>
            <th>Lastname</th>       
            <th>Username</th>
            <th>Edit</th>   </tr> </thead>  "; 
    echo "<tbody>"; 
while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results 
echo "<tr><td>" . $row['first'] . "</td><td>" . $row['last'] . "</td><td>" . $row['username'] . "</td><td>" . "edit" . "</td></tr>";  } 
echo "</tbody> </table>"; //Close the table in HTML
              echo "</div>
            </li>";

这显然是把PHP表格的内容放到了HTML表格中去查看。但我希望能够单击 html 表中每一行旁边的“编辑”选项卡,并能够对该行中的数据进行更改/修改/删除。

这是管理员页面,应该能够为每个用户更改特定详细信息,这基本上就是我想要做的。

我在想我需要利用 while 循环使用一个数组这一事实,我可以从中提取特定的数据行(即 $Row[1]),然后将其放入 $_POST[]?我不确定

最佳答案

您的 MySQL 应该包含一个唯一的 ID。我们暂时称该列为 id。

然后做类似的事情

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results 
echo "<tr>
    <td>" . $row['first'] . "</td>
    <td>" . $row['last'] . "</td>
    <td>" . $row['username'] . " </td>
    <td><a href='editUser.php?id='".$row['id']."'> . "edit" . "</a></td>
</tr>";  } 

然后在你的editUser.php中,添加

$userid = mysql_real_escape_string($_GET['id']);

$query = "SELECT * FROM users WHERE id = ".$userid;
$result = mysql_query($query);

你会编辑吗。

关于PHP - 如何编辑已显示在 HTML 表格中的 PHP 表格的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26787664/

相关文章:

php - 在 Woocommerce 商店页面上隐藏价格

php - 在 cloud9 上同时使用 apache 和 node

javascript - 下拉列表未从数据库填充

php - 用于更新数据库中用户值的脚本不起作用 – session 问题?

php - MySQL SELECT w/PHP 通过日期屏障

php - 多变量传递 URL

php - Facebook 应用程序 - 不请求获取用户数据的权限

php - 非常灵活的应用程序的数据库/应用程序设计

javascript - 打破 css float 和 clear 的默认布局

使用本地存储的 JAVASCRIPT 登录重定向