Afficher le nom des utilisateurs plutôt l'id d'utilisateur de la base de données ?
- iWiGG_2010
- Newbie


- Inscription: Nov 22, 2011
- Messages: 10
- Loc: Australia
- Status: Offline
Bonjour à tous
Je suis en train de faire une page de base de forum. À l'heure actuelle, il affiche l'utilisateur comme un nombre (qui est défini dans la base de données). A, vous vous demandez comment l'obtenir pour afficher le nom des utilisateurs plutôt que leur numéro de base de données ?
Il s'agit de mes tableaux :
« utilisateurs » - user_id, email, nom, mot de passe
« postes » - id, album_id, topic_id, post_creator, post_contact, post_date
« sujets » - id, album_id, topic_id, topic_creator, topic_last_user, topic_date, topic_reply_date, topic_views
Je peux seulement suppose que j'ai que beaucoup besoin pour rejoindre la .user_id « utilisateurs » tableau avec la .post_creator de « postes ». Si je dois utiliser des jointures je cadeaux semblent confondre avec cette partie.
Toute aide fera, merci.
Voici mon code :
view_topic.php
view_topic_parse.php
Je suis en train de faire une page de base de forum. À l'heure actuelle, il affiche l'utilisateur comme un nombre (qui est défini dans la base de données). A, vous vous demandez comment l'obtenir pour afficher le nom des utilisateurs plutôt que leur numéro de base de données ?
Il s'agit de mes tableaux :
« utilisateurs » - user_id, email, nom, mot de passe
« postes » - id, album_id, topic_id, post_creator, post_contact, post_date
« sujets » - id, album_id, topic_id, topic_creator, topic_last_user, topic_date, topic_reply_date, topic_views
Je peux seulement suppose que j'ai que beaucoup besoin pour rejoindre la .user_id « utilisateurs » tableau avec la .post_creator de « postes ». Si je dois utiliser des jointures je cadeaux semblent confondre avec cette partie.
Toute aide fera, merci.
Voici mon code :
view_topic.php
PHP Code: [ Select ]
<?php
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$sql = "SELECT * FROM `topics` WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) == 1) {
echo "<table width='100%'>";
if ($_SESSION['user_id']) {
echo "<tr><td colspan='2'><center><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /></center><hr /><br />";
} else {
echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
while ($row = mysql_fetch_assoc($res)) {
$sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2)) {
echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
<tr><td colspan='2'><hr /></td></tr>";
}
$old_views = $row['topic_views'];
$new_views = $old_views + 1;
$sql3 = "UPDATE `topics` SET `topic_views`='".$new_views."' WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
$res3 = mysql_query($sql3) or die(mysql_error());
}
echo "</table>";
} else {
echo "<p>This topic does not exist</p>";
}
?>
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$sql = "SELECT * FROM `topics` WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) == 1) {
echo "<table width='100%'>";
if ($_SESSION['user_id']) {
echo "<tr><td colspan='2'><center><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /></center><hr /><br />";
} else {
echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
while ($row = mysql_fetch_assoc($res)) {
$sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2)) {
echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
<tr><td colspan='2'><hr /></td></tr>";
}
$old_views = $row['topic_views'];
$new_views = $old_views + 1;
$sql3 = "UPDATE `topics` SET `topic_views`='".$new_views."' WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
$res3 = mysql_query($sql3) or die(mysql_error());
}
echo "</table>";
} else {
echo "<p>This topic does not exist</p>";
}
?>
- <?php
- $cid = $_GET['cid'];
- $tid = $_GET['tid'];
- $sql = "SELECT * FROM `topics` WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
- $res = mysql_query($sql) or die(mysql_error());
- if (mysql_num_rows($res) == 1) {
- echo "<table width='100%'>";
- if ($_SESSION['user_id']) {
- echo "<tr><td colspan='2'><center><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /></center><hr /><br />";
- } else {
- echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
- while ($row = mysql_fetch_assoc($res)) {
- $sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";
- $res2 = mysql_query($sql2) or die(mysql_error());
- while ($row2 = mysql_fetch_assoc($res2)) {
- echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
- <tr><td colspan='2'><hr /></td></tr>";
- }
- $old_views = $row['topic_views'];
- $new_views = $old_views + 1;
- $sql3 = "UPDATE `topics` SET `topic_views`='".$new_views."' WHERE `album_id`='".$cid."' AND id='".$tid."' LIMIT 1";
- $res3 = mysql_query($sql3) or die(mysql_error());
- }
- echo "</table>";
- } else {
- echo "<p>This topic does not exist</p>";
- }
- ?>
view_topic_parse.php
PHP Code: [ Select ]
<?php
if (isset($_POST['topic_submit'])) {
if (($_POST['topic_title'] == "") && ($_POST['topic_content']) == "") {
echo "<p>Sorry, something went wrong! You did not fill in both fields. Please return to the previous plage.</p>";
exit();
} else {
$cid = $_POST['cid'];
$title = $_POST['topic_title'];
$content = $_POST['topic_content'];
$creator = $_SESSION['user_id'];
$sql = "INSERT INTO topics (album_id, topic_title, topic_creator, topic_date, topic_reply_date) VALUES ('".$cid."', '".$title."', '".$creator."', now(), now())";
$res = mysql_query($sql) or die(mysql_error());
$new_topic_id = mysql_insert_id();
$sql2 = "INSERT INTO posts (album_id, topic_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$new_topic_id."', '".$creator."', '".$content."', now())";
$res2 = mysql_query($sql2) or die(mysql_error());
$sql3 = "UPDATE albums SET last_post_date=now(), `last_user_posted`='".$creator."' WHERE `album_id`='".$cid."' LIMIT 1";
$res3 = mysql_query($sql3) or die(mysql_error());
if (($res) && ($res2) && ($res3)) {
header("Location: view_topic.php?cid=".$cid."&tid=".$new_topic_id);
} else {
echo "There was a problem creating your topic. Please try again!";
}
}
}
?>
if (isset($_POST['topic_submit'])) {
if (($_POST['topic_title'] == "") && ($_POST['topic_content']) == "") {
echo "<p>Sorry, something went wrong! You did not fill in both fields. Please return to the previous plage.</p>";
exit();
} else {
$cid = $_POST['cid'];
$title = $_POST['topic_title'];
$content = $_POST['topic_content'];
$creator = $_SESSION['user_id'];
$sql = "INSERT INTO topics (album_id, topic_title, topic_creator, topic_date, topic_reply_date) VALUES ('".$cid."', '".$title."', '".$creator."', now(), now())";
$res = mysql_query($sql) or die(mysql_error());
$new_topic_id = mysql_insert_id();
$sql2 = "INSERT INTO posts (album_id, topic_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$new_topic_id."', '".$creator."', '".$content."', now())";
$res2 = mysql_query($sql2) or die(mysql_error());
$sql3 = "UPDATE albums SET last_post_date=now(), `last_user_posted`='".$creator."' WHERE `album_id`='".$cid."' LIMIT 1";
$res3 = mysql_query($sql3) or die(mysql_error());
if (($res) && ($res2) && ($res3)) {
header("Location: view_topic.php?cid=".$cid."&tid=".$new_topic_id);
} else {
echo "There was a problem creating your topic. Please try again!";
}
}
}
?>
- <?php
- if (isset($_POST['topic_submit'])) {
- if (($_POST['topic_title'] == "") && ($_POST['topic_content']) == "") {
- echo "<p>Sorry, something went wrong! You did not fill in both fields. Please return to the previous plage.</p>";
- exit();
- } else {
- $cid = $_POST['cid'];
- $title = $_POST['topic_title'];
- $content = $_POST['topic_content'];
- $creator = $_SESSION['user_id'];
- $sql = "INSERT INTO topics (album_id, topic_title, topic_creator, topic_date, topic_reply_date) VALUES ('".$cid."', '".$title."', '".$creator."', now(), now())";
- $res = mysql_query($sql) or die(mysql_error());
- $new_topic_id = mysql_insert_id();
- $sql2 = "INSERT INTO posts (album_id, topic_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$new_topic_id."', '".$creator."', '".$content."', now())";
- $res2 = mysql_query($sql2) or die(mysql_error());
- $sql3 = "UPDATE albums SET last_post_date=now(), `last_user_posted`='".$creator."' WHERE `album_id`='".$cid."' LIMIT 1";
- $res3 = mysql_query($sql3) or die(mysql_error());
- if (($res) && ($res2) && ($res3)) {
- header("Location: view_topic.php?cid=".$cid."&tid=".$new_topic_id);
- } else {
- echo "There was a problem creating your topic. Please try again!";
- }
- }
- }
- ?>
- Anonymous
- Bot


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Novembre 22nd, 2011, 7:44 pm
- Bogey
- Bogey


- Inscription: Juil 14, 2005
- Messages: 8212
- Loc: USA
- Status: Offline
Quote:
Code: [ Select ]
echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
<tr><td colspan='2'><hr /></td></tr>";
<tr><td colspan='2'><hr /></td></tr>";
- echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
- <tr><td colspan='2'><hr /></td></tr>";
Je pense que c'est où vous voulez changer le code. Le $row2 ["post_creator"] est l'id utilisateur, droite ? Vous devez faire une requête SQL dans cette boucle pour récupérer le nom d'utilisateur et les utiliser à la place.
Code: [ Select ]
while ($row2 = mysql_fetch_assoc($res2)) {
$user_name = mysql_fetch(mysql_query("SELECT username FROM usertable WHERE userid = {$row2['post_creator']}"));
echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
<tr><td colspan='2'><hr /></td></tr>";
}
$user_name = mysql_fetch(mysql_query("SELECT username FROM usertable WHERE userid = {$row2['post_creator']}"));
echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
<tr><td colspan='2'><hr /></td></tr>";
}
- while ($row2 = mysql_fetch_assoc($res2)) {
- $user_name = mysql_fetch(mysql_query("SELECT username FROM usertable WHERE userid = {$row2['post_creator']}"));
- echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
- <tr><td colspan='2'><hr /></td></tr>";
- }
Je pense que c'est ce que vous voulez partir rapidement regarder.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- iWiGG_2010
- Newbie


- Inscription: Nov 22, 2011
- Messages: 10
- Loc: Australia
- Status: Offline
Merci Bogey,
Il semble ressembler à ce que j'essaie de le faire, mais il toujours ne pas montrant le nom (seulement l'id d'utilisateur). Oui $row2 ["post_creator"] est l'id utilisateurs.
Peut-être que je fais quelque chose ? Désolé vraiment nouveau à php/mysql.
Il semble ressembler à ce que j'essaie de le faire, mais il toujours ne pas montrant le nom (seulement l'id d'utilisateur). Oui $row2 ["post_creator"] est l'id utilisateurs.
Peut-être que je fais quelque chose ? Désolé vraiment nouveau à php/mysql.
PHP Code: [ Select ]
echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
while ($row = mysql_fetch_assoc($res)) {
$sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2)) {
$user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
<tr><td colspan='2'><hr /></td></tr>";
}
while ($row = mysql_fetch_assoc($res)) {
$sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2)) {
$user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
<tr><td colspan='2'><hr /></td></tr>";
}
- echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
- while ($row = mysql_fetch_assoc($res)) {
- $sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";
- $res2 = mysql_query($sql2) or die(mysql_error());
- while ($row2 = mysql_fetch_assoc($res2)) {
- $user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
- echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$row2['post_creator']." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
- <tr><td colspan='2'><hr /></td></tr>";
- }
- Bogey
- Bogey


- Inscription: Juil 14, 2005
- Messages: 8212
- Loc: USA
- Status: Offline
You got changer les $row2 ["post_creator"] pour $user_name sur la ligne 6...j'ai oublié de changer cela dans mon exemple sur mon premier post.
PHP Code: [ Select ]
echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
while ($row = mysql_fetch_assoc($res)) {
$sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2)) {
$user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$user_name." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
<tr><td colspan='2'><hr /></td></tr>";
}
while ($row = mysql_fetch_assoc($res)) {
$sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2)) {
$user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$user_name." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
<tr><td colspan='2'><hr /></td></tr>";
}
- echo "<br /><tr><td colspan='2'><p>Please Log in to add a comment</p></td></tr>"; }
- while ($row = mysql_fetch_assoc($res)) {
- $sql2 = "SELECT * FROM `posts` WHERE `album_id`='".$cid."' AND `topic_id`='".$tid."' ORDER BY `post_date` DESC";
- $res2 = mysql_query($sql2) or die(mysql_error());
- while ($row2 = mysql_fetch_assoc($res2)) {
- $user_name = mysql_fetch_assoc(mysql_query("SELECT name FROM users WHERE user_id = {$row2['post_creator']}"));
- echo "<tr><td valign='top' style='border: 1px sold #FFF;'><div style='min-height: 125px;'><br /><p>".$row['topic_title']."<br /><br />by ".$user_name." - ".$row2['post_date']."<br />".$row2['post_content']."</p></div></td>
- <tr><td colspan='2'><hr /></td></tr>";
- }
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
Page 1 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
Afficher de l'information
- Total des messages de ce sujet: 4 messages
- Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 114 invités
- Vous ne pouvez pas poster de nouveaux sujets
- Vous ne pouvez pas répondre aux sujets
- Vous ne pouvez pas éditer vos messages
- Vous ne pouvez pas supprimer vos messages
- Vous ne pouvez pas joindre des fichiers
