如何通过单击“编辑”按钮编辑php表中的记录?

时间:2017-10-07 11:40:20

标签: php html mysql

我是PHP的新手,还在学习这门语言。我创建了一个php页面,可以在表格中显示我数据库中的所有记录(称为view_users.php)。我还创建了两个按钮“编辑”和“删除”。

我希望能够在单击附加到其上的“编辑”按钮时编辑特定行上的记录。有没有办法可以在同一页面上执行此操作(view_users.php)。

以下是view_users.php的源代码

   <html>
   <head lang="en">
   <meta charset="UTF-8">
   <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css"> <!--css file link in bootstrap folder-->
   <title>View Users</title>
    </head>
    <style>
.login-panel {
    margin-top: 150px;
}
.table {
    margin-top: 50px;

    }

    </style>

     <body>

       <div class="table-scrol">
       <h1 align="center">All the Users</h1>

          <div class="table-responsive"><!--this is used for responsive display in mobile and other devices-->


         <table class="table table-bordered table-hover table-striped" style="table-layout: fixed">
    <thead>
    <tr>

        <th>User Id</th>
        <th>User Name</th>
        <th>User E-mail</th>
        <th>User Pass</th>
        <th>Action</th>
    </tr>
    </thead>

    <?php
    include("database/db_conection.php");
    $view_users_query="select * from users";//select query for viewing users.
     $run=mysqli_query($dbcon,$view_users_query);//here run the sql query.

    while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
    {
        $user_id=$row[0];
        $user_name=$row[1];
        $user_email=$row[2];
        $user_pass=$row[3];



    ?>

      <tr>
     <!--here showing results in the table -->
        <td><?php echo $user_id;  ?></td>
        <td><?php echo $user_name;  ?></td>
        <td><?php echo $user_email;  ?></td>
        <td><?php echo $user_pass;  ?></td>
        <td> <button type="button" class="btn btn-primary">Edit</button>
        <a href="delete.php?del=<?php echo $user_id ?>"  onclick="return confirm('Are you sure?')"><button class="btn btn-danger">Delete</button></a></td>
    </tr>

       <?php } ?>

      </table>
       </div>
      </div>


     </body>

  </html>

2 个答案:

答案 0 :(得分:0)

制作你的编辑按钮:

if(isset($_POST['edit']))
{
    $row = $_POST['edit']
    ....have form to edit this row
    ....update row $i in database
    ....submit button
}

$ i是while循环中记录的mysql行的当前id。

然后在view_users.php页面的顶部添加如下内容:

tasks.json

答案 1 :(得分:0)

在HTML页面中,您可以在操作列

中添加以下代码

基本上您通过GET参数发送相应的 ID

<td>
    <a href="editpage.php?id="<?php echo $userid; ?>>Edit</a>
</td>

在您的PHP代码中,您可以按如下方式访问 id

<?php

if(!empty($_GET['id'])){ //Make sure that id parameter is set in your url
    $userid = $filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT); //Now use the 
    //$userid = $_GET['id']; //The above line is safer then this line

    //Now do what ever you want with the id
}