0

I have a user table where I have to display a delete button and I need to make sure the current user cannot delete themselves.
In order to do that, I wrote some code as shown below, but it doesn't work.

The user table appears with the delete button in all the users, but I need to make sure the current user cannot delete their own user account.

while($row = mysqli_fetch_array($result)) {
$output .='<tr>  
            <td>'.$row["user_id"].'</td>  
            <td>'.$row["fullname"].' </td>
            <td>'.$row["user_role"].'</td>
            <td>'.$row["username"].'</td>
            <td>'.$row["password"].'</td> 
            <td>'.$row["branch_id"].'</td>
            <td>'.$row["registered_date"].'</td>  
            <td>
                <button id="'.$row["user_id"].'" class="btn btn-warning btn-xs edit_data"><i class="fa fa-edit "></i></button>&nbsp;
            if('.$_SESSION['username'].'!='.$row["username"].') {
            <a id="'.$row["user_id"].'"  class="btn btn-danger btn-xs delete"><i class="fa fa-times"></i></a> } </td> 
        </tr>';
}
$output .= '</table>';
echo $output;
2
  • why is jquery tagged?? removing it now. Commented Nov 29, 2017 at 11:47
  • need some answer bro?? Commented Nov 29, 2017 at 11:57

2 Answers 2

1

You can not execute PHP in a string, you need to split your string creation.

while ($row = mysqli_fetch_array($result)) {
    $output .='<tr>  
                <td>'.$row["user_id"].'</td>  
                <td>'.$row["fullname"].' </td>
                <td>'.$row["user_role"].'</td>
                <td>'.$row["username"].'</td>
                <td>'.$row["password"].'</td> 
                <td>'.$row["branch_id"].'</td>
                <td>'.$row["registered_date"].'</td>  
                <td>
                    <button id="'.$row["user_id"].'" class="btn btn-warning btn-xs edit_data"><i class="fa fa-edit "></i></button>';
                    if ($_SESSION['username'] != $row["username"]) {
                        $output .= '<a id="'.$row["user_id"].'"  class="btn btn-danger btn-xs delete"><i class="fa fa-times"></i></a>';
                    }
    $output .= '</td></tr>';
}
$output .= '</table>';
echo $output;
Sign up to request clarification or add additional context in comments.

1 Comment

@sagarneupane don't forget the green check:)
0

Since you output your variable right after the loop - you can do the following

<table>
<?php
while($row = mysqli_fetch_array($result))
{
?>
    <tr>
        <td><?=$row['user_id']; ?></td>
        <td><?=$row['fullname']; ?></td>
        <td><?=$row['user_role']; ?></td>
        <td><?=$row['username']; ?></td>
        <td><?=$row['password']; ?></td>
        <td><?=$row['branch_id']; ?></td>
        <td><?=$row['registered_date']; ?></td>
        <td>
        <?php
        if($_SESSION['username'] != $row['username'])
        {
        ?>
         <a id="<?=$row['user_id']; ?>"  class="btn btn-danger btn-xs delete"><i class="fa fa-times"></i></a>
        <?php
        }
        ?>
        </td>
    </tr>
<?php
}
?>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.