garrensilverwing
New Member
- Messages
- 148
- Reaction score
- 0
- Points
- 0
hey guys i am trying to write a code with php/mysql that will create a table inside my comments database...however when i run the code the first time i visit the page it creates the table but if i go back to the page it says table already exists when i want it to display the comments left and the post comment input box
here is my code:
where
$globals['domain'] = my website url (since i test on my computer @ localhost it was the easiest way i could figure out how to rapdily change links)
$thread = the name of the page it will be displayed on also the name of the table
$username = the username of whoever is currently posting a comment
$text = the text of the comment
and hopefully the rest is self explanatory, thanks again in advance for the help
here is my code:
Code:
function pagecomments($thread)
{
$username = "frostbit_admin";
$password = "***";
$server = "localhost";
$commentsdatabase="frostbit_comments";
$mysqlconnection = mysql_connect($server, $username, $password);
if (!$mysqlconnection)
{
die('There was a problem connecting to the mysql server. Error returned: '. mysql_error());
}
$commentsdatabaseconnection = mysql_select_db($commentsdatabase);
if (!$commentsdatabaseconnection)
{
die('There was a problem using that mysql database. Error returned: '. mysql_error());
}
$sql="CREATE TABLE IF NOT EXISTS `$thread`(`id` int(16) NOT NULL AUTO_INCREMENT,`posted` datetime NOT NULL,`user` varchar(16) NOT NULL,`comment` text NOT NULL,`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,`modifiedby` varchar(16) NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=13;";
mysql_query($sql) or die(mysql_error());
}
function displaycomments($thread)
{
if(isset($_SESSION['user']))
{
$sql="SELECT * FROM `$thread` ORDER BY `posted` DESC;";
$query=mysql_query($sql) or die(mysql_error());
$commentcount=count($query);
while($row = mysql_fetch_array($query))
{
$username=$row['user'];
$text=$row['comment'];
echo "<p class=\"desctxt\"><i>Posted by: <strong>$username</strong> at ".$row['posted']."</i></p><br>";
echo "<div class=\"commentdesign\">$text</div>";
echo "<p class=\"desctxt\" style=\"margin-bottom: .5em;\"><i>Last Modified: ".$row['modified']."</i></p>";
commentoptions($username,$text);
}
}
else
{
echo "<i>You must be logged in to view comments.</i>";
}
}
function commentoptions($username,$text)
{
?>
<div id="commentwrapper" style="position:relative; height:3em; width:25em; margin-right:auto; margin-left:auto; text-align:left;">
<div style="position: relative; z-index: 1; width: 25em; height: 1em;" id="options">
<div style="position: relative; z-index: 1; float: left;" id="viewer">
<?php
echo "<img alt=\"reply\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_add.png\" />";
echo "<img alt=\"quote\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_quote.png\" />";
echo "<img alt=\"report\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_warning.png\" />";
echo "</div>";
if($username == $_SESSION['user'])
{
echo "<div style=\"position: relative; z-index: 1; float: left;\" id=\"poster\">";
echo "<img alt=\"edit\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_edit.png\" />";
echo "<img alt=\"notify\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_email.png\" />";
echo "</div>";
}
if($_SESSION['admin'] == 1)
{
echo "<div style=\"position: relative; z-index: 1; float: right;\" id=\"admin\">";
echo "<img alt=\"delete\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_remove.png\" />";
echo "<img alt=\"warn\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_error.png\" />";
echo "<img alt=\"lock\" src=\"http://".$GLOBALS['domain']."/images/commenticons/lock.png\" />";
echo "<img alt=\"sticky\" src=\"http://".$GLOBALS['domain']."/images/commenticons/bulb_on.png\" />";
echo "<img alt=\"edit\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_edit.png\" />";
echo "</div>";
}
?>
</div>
</div>
where
$globals['domain'] = my website url (since i test on my computer @ localhost it was the easiest way i could figure out how to rapdily change links)
$thread = the name of the page it will be displayed on also the name of the table
$username = the username of whoever is currently posting a comment
$text = the text of the comment
and hopefully the rest is self explanatory, thanks again in advance for the help
Last edited: