Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Björn Norén
Björn Norén
9,569 Points

Edit & Create Multiple Users

Hi! I have this easy page where you can choose a username and then delete it, so it's quite a simple code. Here's a link: http://studenter.miun.se/~bjno1501/dt057g/php/moment2/practise/sessions.php

Could you show/tell me how I can add like multiple users, so it adds on line after line?

And second, how do I do if I want to edit a user but not delete it first? So there's a button at the right of every user so you have a option to edit it?

Thanks! Sorry for my english, my native language is Swedish

Here's the code:

--- sessions.php ---

<!DOCTYPE html>
<html> <head> <meta charset="utf-8"> </head> <body> <h1>Sessions</h1>

<?php 
    session_start(); 

    if(isset($_POST['user'])) { 
        $user = $_POST['user'];             
        $_SESSION['username'] = $user; 
    } 

    if(isset($_SESSION['username'])) { 

        echo "<p class='message box'>Sessions-username: " . $_SESSION['username'] . " " . 
        "<a href='logout.php' class='rightbutton'><button class='btn btn-danger'>Log out</button></a>" . 
        "</p>\n"; 
    } 
?> 

<form method="post" class="form-inline"> 

    Name: <input type="text" name="user" class="form-control"> 
    <input type="submit" value="Save user" class="btn btn-info"> 

</form> 
</body> 

</html> <?php

--- logout.php ---

<?php session_start(); session_destroy();

header("Location: sessions.php");

2 Answers

Jeremy Canela
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jeremy Canela
Full Stack JavaScript Techdegree Graduate 30,766 Points

To edit the user, just have a page for example: edit.php. In that page, just have a form so the user can edit their information. I'm guessing the user will only be able to edit their username, so once they submit the form, just do something like this:

    if(isset($_POST['submit']) && !empty($_POST['inputUsername'])) {
    // If the save button is clicked and if the input is not empty
        $_SESSION['username'] = $_POST['inputUsername'];
    }

In edit.php:

    <form action="" method="post">
        <input type="text" name="inputUsername">
        <button type="submit" name="submit">Save</button>
    </form>

Any problems please comment below :)

Björn Norén
Björn Norén
9,569 Points

Hi! Thanks for helping me out :) I copied your code and added it to a new page named it edit.php.

I have a problem adding the code, how should I include it? I tried to add the edit-page here in the sessions-page where the text comes up when a new user is added:

<!DOCTYPE html>
<html> <head> <meta charset="utf-8"> </head> <body> <h1>Sessions</h1>

<?php
    session_start();

    if(isset($_POST['user'])) {
        $user = $_POST['user'];            
        $_SESSION['username'] = $user;
    }

    if(isset($_SESSION['username'])) {

        echo "<p class='message box'>Sessions-username: " . $_SESSION['username'] . " " .
        "<a href='logout.php' class='rightbutton'><button class='btn btn-danger'>Log out</button></a>
         <a href='edit.php'><button>Edit</button></a>" .
        "</p>\n";
    }
?>

<form method="post" class="form-inline">

    Name: <input type="text" name="user" class="form-control">
    <input type="submit" value="Save user" class="btn btn-info">

</form>
</body>

</html> <?php