2017-10-07 89 views
-1

我是PHP新手,仍在学习语言。我创建了一个php页面,可以显示我的数据库中的所有记录(称为view_users.php)。我还创建了两个按钮“编辑”和“删除”。如何通过单击“编辑”按钮来编辑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> 

回答

0

在你的HTML页面,您可以在动作栏中添加以下代码

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

<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 
} 
0

我的源代码使你的编辑按钮:

<button type="button" name="<?php echo'edit[$i]'; ?>" class="btn btn-primary">Edit</button> 

$ i是记录在mysql while循环行的电流id 。

然后在你的view_users.php页面的顶部添加类似:

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

对不起它不是一个很好的答案,但希望你的想法。 – Kaiwen