BeKustom
BeKustom

Reputation: 47

Give logged in user delete option only

I am writing a script that allows users to create teams, send contracts to others that are registered, so on and so forth. I want the logged in user to be able to withdraw from a team. The html tables are dynamically populated from mysql using php. I can get it to apply my delete option to all the users within the <td></td> but not just the one that is logged in. Here is some code snippets to help I hope. Basically I want only the user that is logged in to have the delete option.

    Id   Player
    1    User 1 - No Delete Option
    2    User 2 - Delete Option (Is the logged in user)
    3    User 3 - No Delete Option

    session_start();
    $get_users_name = $_SESSION['username_u'];

    $sql = ("SELECT id, username FROM user WHERE username='$get_users_name'");
    $result = mysql_query($sql) or die(mysql_error());

    while ($row = mysql_fetch_array($result)) {
    $grab_id = $row['id'];
    $grab_user = $row['username'];

//the rest of the table generation done here such as <tr> etc

    echo "<td>";
    if ($grab_user == $get_users_name) { 
    echo '<a href="user_delete.php?id='.$grab_id.'" onClick="return confirm(\'Are you sure you want to withdrawl from the team?\')">'.$grab_user.'</a>';
    } 
    else
    { 
    echo $grab_user;
    }
    echo "</td>";

//the rest of the table generation done here such as </tr> etc

}

**Edited to fix echo problem caught by @easteregg

Upvotes: 0

Views: 224

Answers (1)

easteregg
easteregg

Reputation: 509

Just be sure you will style your code for readability ,then you will notice that you have your if condition inside the echo ;)

Try this it should work!

echo "<td>";

if ($grab_user == $get_users_name)  {
    echo '<a href="user_delete.php?id='.$grab_id.'" onClick="return confirm(\'Are you sure you want to withdrawl from the team?\')">'.$grab_user.'</a>';
} else {
    echo $grab_user;
}

echo "</td>";

Be aware of the fact, that you should check again in the user_delete.php if a user has the right to delete something , otherwise you will run into some strange sort of trouble ;)

Upvotes: 1

Related Questions