-1

Here is my code. There's is a "Edit" link. I want to update data and save with that link

    $op_sql = "SELECT * FROM tbl_users"; 
    $result = $con->query($op_sql); 
    if ($result->rowCount() > 0) 
    { 

    echo "<table class='table' style='margin-left: 301px; margin-top: 10px; background-color: white;'>-; 
    echo "<tr>"; 
    echo "<th>ID</th>"; 
    echo "<th>Name</th>"; 
    echo "<th>Email</th>"; 
    echo "<th>Date of Birth<th>"; 
    echo "<th></th>"; echo "</tr>"; 
    while($row = $result->fetch())
    { 

    echo "<tr>"; 
    echo "<td>" . $rowrID1 . "</td>"; 
    echo "<td>" . $row['Name'] . "</td>"; 
    echo "<td>" . $row['Email']. "</td>"; 
    echo "<td>" . $row['Date of Birth'] . "</td>";
    echo "<td><a href='?id=".$row['ID']."'>Edit</a></td>"; 
    echo "</tr>"; 
    } 
    echo "</table>"; unset($result); 
JYoThI
  • 11,977
  • 1
  • 11
  • 26
Khant Zin
  • 27
  • 1
  • 2
  • 10

2 Answers2

1

You can add the user_edit.php url in anchor tag href to edit the user details like this.

echo "<td><a href='user_edit.php?id=".$row['ID']."'>Edit</a></td>"; 

user_edit.php

Here you can get the id of specific user and get the details from database and create update form like this.

    if(isset($_GET['id']) && $_GET['id']!='')
    {
        $op_sql = "SELECT * FROM tbl_users where id=$_GET['id']"; 
        $result = $con->query($op_sql); 
        if ($result->rowCount() > 0) 
        {  
            $row = $result->fetch();
        ?>
           <form action="user_update.php" method="post">
           <input type="text" name="Name" value="<?php $row['Name'] ?>" />
           .
           .
           <input type="hidden" name="id" value="$_GET['id']" />
           .
           .
           <input type="submit" name="update" value="update" />

        <?php

        }
    }

user_update.php

Here you can get the updated form data and update in database

    if(isset($_POST['update']) && isset($_POST['id']) && $_POST['id']!='' )
    {

        // here you can get the updated form data and update in database 

        // update user set Name=$_POST['Name'],.... where id=$_POST['id'];

    }

NOTE :

Try to use Prepared statement or PDO

JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • if my answer is useful mark it with green tick its useful for future user reference .@KhantZin – JYoThI Apr 11 '17 at 07:17
1

I am giving an answer from This question you can use the same answer in your context here I'm providing the code from the accepted answer.

You can use a get method here in place of the post according to your situation.

HTML form:

<form method="post" action="handler.php">

    <button type="submit" name="passed" id="passed" class="btn btn-success btn-flat"><i class="fa fa-check"></i></button>
    <button type="submit" name="insufficient" id="insufficient" class="btn btn-danger btn-flat"><i class="fa fa-times"></i></button>

</form>

PHP:

<?php 

// db connection

if(isset($_POST['passed'])){

    $allowed = mysqli_query($conn," UPDATE users SET Access = "1" WHERE id = '27' ");

}

if(isset($_POST['insufficient'])){

    $notallowed = mysqli_query($conn," UPDATE users SET Access = "0" WHERE id = '453' ");

}

Community
  • 1
  • 1
Black Mamba
  • 13,632
  • 6
  • 82
  • 105