john010117 Posted May 4, 2007 Share Posted May 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244928 Share on other sites More sharing options...
Shaun13 Posted May 4, 2007 Author Share Posted May 4, 2007 It works! Thanks so much. One more thing though. When you are logged out it says "You have encountered an error." instead of "Login | Register... Also I will read the link you gave me. ~Shaun Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244935 Share on other sites More sharing options...
john010117 Posted May 4, 2007 Share Posted May 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244938 Share on other sites More sharing options...
Shaun13 Posted May 4, 2007 Author Share Posted May 4, 2007 Uh oh, I get a parse error. Parse error: syntax error, unexpected $end in /var/www/shaunsnetwork/shaunsboards/index.php on line 77 Also, do you think you could encorperate that anti mysql injection stuff into the pages? I would appreciate it greatly. ~Shaun Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244940 Share on other sites More sharing options...
john010117 Posted May 4, 2007 Share Posted May 4, 2007 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). Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244943 Share on other sites More sharing options...
Shaun13 Posted May 4, 2007 Author Share Posted May 4, 2007 I am really sorry about bothering you again, but is still doesnt show up. The error message is gone, but the Register | Login links don't show up. ~Shaun Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244948 Share on other sites More sharing options...
john010117 Posted May 4, 2007 Share Posted May 4, 2007 <?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> Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244951 Share on other sites More sharing options...
Shaun13 Posted May 4, 2007 Author Share Posted May 4, 2007 Now I get that parse error about the $end. Why is this so confusing?????? ~Shaun Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244954 Share on other sites More sharing options...
john010117 Posted May 4, 2007 Share Posted May 4, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244956 Share on other sites More sharing options...
Shaun13 Posted May 4, 2007 Author Share Posted May 4, 2007 Thank you sooo much. I appreciate all of your time and effort. I guaruntee I will be back here with more problems in the future. ~Shaun Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244959 Share on other sites More sharing options...
john010117 Posted May 4, 2007 Share Posted May 4, 2007 No problem. Make sure to mark this topic as "solved" (link located on the bottom of this page). Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244964 Share on other sites More sharing options...
Liquid Fire Posted May 4, 2007 Share Posted May 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-244974 Share on other sites More sharing options...
Shaun13 Posted May 4, 2007 Author Share Posted May 4, 2007 Ok, how would I register $mydisplayname to the session so I could do $_SESSION['mydisplayname']; and get the display name throughout the rest of the session? Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-245000 Share on other sites More sharing options...
Liquid Fire Posted May 4, 2007 Share Posted May 4, 2007 just do $_SESSION['mydisplayname'] = $mydisplayname; Quote Link to comment https://forums.phpfreaks.com/topic/49903-quick-php-question/page/2/#findComment-245009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.