I have 2 tables (Users, Wall). The UserID in the Wall table is a foreign key. How would I go about fetching the users details using this? (I want to fetch the users Forename and Surname who posted the message.)
Users Table:
Wall Table:
EDIT: I cannot figure out how to show the data.
<?php include('config.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Alpha</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
// Logged IN
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Email'])) {
// Post to Database
if(!empty($_POST['message']))
{
$message = mysql_real_escape_string($_POST['message']);
$postmessage = mysql_query("INSERT INTO Wall (Message, UserID) VALUES('".$message."', '".$_SESSION['UserID']."')");
}
// Collet Latest Posts
$result = mysql_query('SELECT Message, UserID
FROM Wall
ORDER BY MessageID DESC
LIMIT 20') or die('Invalid query: ' . mysql_error());
// Collet Post User
$query = mysql_query('SELECT Forename, Surname FROM Users INNER JOIN Wall ON Users.UserID = Wall.UserID;') or die('Invalid query: ' . mysql_error());
?>
<div id ="container">
<div id="insideleft">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="profile.php">Edit Profile</a></li>
<li><a href="wall.php">Community Wall</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</div>
<div id="insideright">
<h1>Community Wall</h1>
<br />
<form method="post" action="wall.php" name="wallpost" id="wallpost">
<label for="message" class="message">Message: </label> <input type="text" name="message" id="message" class="message"/>
<input type="submit" name="messagesub" id="messagesub" value="Post" /><br /><br />
</fieldset>
</form>
<?php while ($row = mysql_fetch_assoc($result)) { ?>
<p></p>
<p><?=stripslashes($row['Message'])?></p><br />
<?php
} ?>
</div>
</div>
<?php
}
//else {echo "<meta http-equiv='refresh' content='0;index.php'>";}
?>
</body>
</html>
As you can see I am outputting the message but I have no idea how to output the Forename and Surname of the poster.
$hostname = 'localhost';
$username = 'username';
$password = 'password';
$dbname = 'database';
$db = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$query = <<<QUERY
SELECT Forename, Surname
FROM Users
INNER JOIN Wall ON Users.UserID = Wall.UserID;
QUERY;
$statement = $db->query($query);
$rows = $statement->fetch(PDO::FETCH_ASSOC);
print_r($rows);
$db = null;
EDIT: Given the new information, you should combine your queries into one.
<?php include('config.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Alpha</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
// Logged IN
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Email'])) {
// Post to Database
if(!empty($_POST['message']))
{
$message = mysql_real_escape_string($_POST['message']);
$postmessage = mysql_query("INSERT INTO Wall (Message, UserID) VALUES('".$message."', '".$_SESSION['UserID']."')");
}
// Collet Latest Posts
$query = <<<QUERY
SELECT Users.UserID, Message, Forename, Surname
FROM Users
INNER JOIN Wall ON Users.UserID = Wall.UserID;
ORDER BY MessageID DESC
LIMIT 20;
QUERY;
$result = mysql_query($query) or die('Invalid query: ' . mysql_error());
// Collet Post User
?>
<div id ="container">
<div id="insideleft">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="profile.php">Edit Profile</a></li>
<li><a href="wall.php">Community Wall</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</div>
<div id="insideright">
<h1>Community Wall</h1>
<br />
<form method="post" action="wall.php" name="wallpost" id="wallpost">
<label for="message" class="message">Message: </label> <input type="text" name="message" id="message" class="message"/>
<input type="submit" name="messagesub" id="messagesub" value="Post" /><br /><br />
</fieldset>
</form>
<?php while ($row = mysql_fetch_assoc($result)) { ?>
<p></p>
<p>
<?php
echo "Message: ".stripslashes($row['Message'])."<br />";
echo "Name: {$row['Surname']}, {$row['Forename']}";
?>
</p><br />
<?php
} ?>
</div>
</div>
<?php
}
//else {echo "<meta http-equiv='refresh' content='0;index.php'>";}
?>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With