Jump to content

Recommended Posts

index.php

 

<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(isset($_SESSION['myusername'])){
      
         echo "Welcome , $displayname | <a href='logout.php'>Logout</a>";
      }
      else
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
else {
   echo "You have encountered an error.";
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['myusername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

 

This should work. But I see that you didn't put any kind of security measures in your code. Read this after you get your code to work.

index.php

<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(isset($_SESSION['myusername'])){
      
         echo "Welcome , $displayname | <a href='logout.php'>Logout</a>";
      }
      elseif(!isset($_SESSION['myusername'])) {
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['myusername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

 

This should take care of it.

index.php

<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(isset($_SESSION['myusername'])){
      
         echo "Welcome , $displayname | <a href='logout.php'>Logout</a>";
      }
      elseif(!isset($_SESSION['myusername'])) {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['myusername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

 

I just told you about the page to include that in your other codes (if you have any).

<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(isset($_SESSION['myusername'])){
      
         echo "Welcome , $displayname | <a href='logout.php'>Logout</a>";
      }
      elseif(!isset($_SESSION['myusername'])) {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
elseif (!isset($_SESSION['myusername'])) {
   echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['myusername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

A simple bracket...

 

<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(isset($_SESSION['myusername'])){
      
         echo "Welcome , $displayname | <a href='logout.php'>Logout</a>";
      }
      elseif(!isset($_SESSION['myusername'])) {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
elseif (!isset($_SESSION['myusername'])) {
   echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['myusername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

Here, if you are building a forum system, i would suggest using or build(like i did) and database class.  This will make you code less cluttered with the mysql stuff.  I could eliminate this code

<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(isset($_SESSION['myusername'])){
      
         echo "Welcome , $displayname | <a href='logout.php'>Logout</a>";
      }
      elseif(!isset($_SESSION['myusername'])) {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
elseif (!isset($_SESSION['myusername'])) {
   echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['myusername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

 

to

 

<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

$database_object = new CDatabase($host, $username, $password, $db_name);

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result = $database_object->GetAllArray($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$displayname = $database_object->GetOne($sql);
if(isset($_SESSION['myusername'])){

echo "Welcome , $displayname | <a href='logout.php'>Logout</a>";
}
elseif(!isset($_SESSION['myusername'])) {
echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
}
elseif (!isset($_SESSION['myusername'])) {
   echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
foreach($result as $key => $row){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $row['id']; ?>"><? echo $row['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $row['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $row['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $row['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $row['datetime']; ?></td>
</tr>

<?php
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['myusername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

 

Granted that is only 6 lines of code but i think the foreach statement are easier to use the the while statments.  This also will eliminate simple mistake which using the mysql functions.  Also with this you gain a alot of stuff.  Lets say you wan tot now use MSSQL database.  With your current code it would take major work to convert it to that but with a class you can just expand the class to have a variable called $database_type and have it so that when you change that is it will do what it needs to to use that database but the code you wrote will stay the same as teh interface will not change(there are main database classes that does this already).  Also to get 1 value from a database the current way would take a little extra code where using a class would take 1 line.  just something to think about. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.