This is my first post, I'm trying to make chat-room for my browser game and I want to make when user register on my browser game to be able when go to chat room to use that username. This is error "Notice: Undefined index: id in *:*****php." and I keep hitting the wall this is my code:
<html>
<head>
<meta http-equiv="refresh" content="2">
</head>
<body>
<?php
//Database connection
$*********= "******";
$**********= "*****";
$**********= "*******";
$*****= "*********";
//Create Connection
$conn = new mysqli($********, $*******, $*******, $****);
//Check Connection
if($conn->connect_error)
{
die("Connection error: ".conn>connect_error);
}
if(isset($_SESSION['loggedin']))
{
$username = $_SESSION['loggedin'];
//GET USER DATA
$query = "SELECT id FROM users WHERE username = '$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
//USERDATA
$userId = $row['id'];
}
//query the database
$query = "SELECT messageid, message, timestamp FROM messages ORDER by
timestamp DESC";
if ($result = mysqli_query($conn, $query))
{
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result))
{
echo $row['id'].' says: '.$row['message'].'</br>';
}
mysqli_free_result($result);
}
/* close connection */
mysqli_close($conn);
?>
</body>
</html>
This is picture of error:
it seems messages to work at least
P.S I'm new to programming and I was looking for this problem over the internet, can't find it. So I ask you smart people to help me pls :)
Like @Suneel Kumar commented on your question. Your error lies here:
$query = "SELECT messageid, message, timestamp FROM messages ORDER by timestamp DESC";
You are selecting the following rows from your database: messageid, message, timestamp
And you are trying to echo an undefined index of an array "id".
echo $row['id']
Either you have to change this to
echo $row['messageid']
or you have to query the id row from the table aswell.
Edit:
A good "debugging" tip for developing in PHP would also be to try something like var_dump($row). This would dump the entire array object in your browser, so that you can see all the key values that exists in the object you are working with.