I have a script where it's displaying user info on a leader board. It's grabbing each user's display info through the 'registrations' table as shown in the top sql, however their back-end info (username, user id, ip address etc) is stored in the 'users' table. The registrations table has a column 'user_id', which I'm trying to link with the users table which can be seen after the foreach
.
The goal is to have the username (stored in 'users') to be displayed for each user - which is shown at <td class='center'>" . $userdata['username'] . "</td>
. However, the username isn't displaying.
At the bottom, I'm trying to insert a record to another table which also uses this same username value, however it inserts as an empty string. Please note the INSERT
should only insert for the user that was clicked on.. can this even be achieved?
-Also, I am aware this PHP isn't the best and should be using prepared statements etc, however that is not the current goal at the moment.
<?php
$sql = mysqli_query($DB, "SELECT * FROM registrations LIMIT 5");
foreach ($sql as $row)
{
$userdata = mysqli_fetch_assoc(mysqli_query($DB, "SELECT * FROM users WHERE id='" . $row['user_id'] . "'"));
echo "
<tr class='gradeX'>
<td class='center'>" . $userdata['username'] . "</td>
<td class='center'>" . $row['balance'] . "</td>
<td class='center'>" . $row['wins'] . " - " . $row['losses'] . "</td>
<td class='center'><form method='post' action=''><input type='submit' class='btn btn-success' name='invite' value='Request Game'/></form></td>
<td class='center'><a href='' class='btn btn-success'>WATCH STREAM</a></td>
</tr>";
}
if (isset($_POST['invite']))
{
$query = "INSERT INTO matches (id,sender,receiver,time,console,rank_sender,rank_receiver,status) values ('0', '" . $_SESSION['username'] . "', '" . $userdata['username'] . "', '" . time() . "', '" . $row['type'] . "', '" . $GETD['rank'] . "', '$rank', 'pending')";
mysqli_query($DB, $query);
}
?>
Thanks for any help :)