0

Users can add their workouts into a database. There will have more than one workout per user. How can i display the workout names for the logged in user, underneath each other? I have no problem adding the workouts. When I echo the names of the added workouts, it display right next to each other like "workout1workout2". I want to be able to display the workouts underneath each other. I have no idea how to do it. It would be best if I could display each workout as a button.

$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
    echo $data['workout'];
}
Andronicus
  • 25,419
  • 17
  • 47
  • 88
user11787018
  • 25
  • 1
  • 5
  • Offtopic: Don't ever assume the SESSION array to be safe because it's not especially on shared hosts (with bad configs), [this post of mine](https://stackoverflow.com/questions/18262878/how-to-prevent-php-sessions-being-shared-between-different-apache-vhosts/18263063#18263063) explains why – Raymond Nijland Jul 24 '19 at 12:02
  • Always use prepared statements when quering a database make no exceptions even when it seams to be "safe" ... See [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) – Raymond Nijland Jul 24 '19 at 12:07

2 Answers2

0

$query = "SELECT * FROM info WHERE username='$_SESSION[username]' Group By username";

0

Firstly, you shouldn't put that username in query like that. Look into SQL Injection and how to avoid that in PHP.

As for displaying buttons/workouts. You can embed HTML code inside the echo statement like below. You can add any html like that and it will render HTML.

   $query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
   $results = mysqli_query($db, $query);
   while($data = mysqli_fetch_array($results)) {
   echo "<button>$data['workout']</button></br>";
   }
Sarosh Khatana
  • 501
  • 6
  • 16