The error that I get is that it won't insert the stuff that I write to the database at all. So I guess the connection must be wrong. If someone could check my codes and tell me what might be wrong? That would make me sooooo happy!!! So here's my codes:
##Here's my code for that dbconnect.php page
Code: Select all
if (!($mylink = mysql_connect("localhost","myusername","mypassword")))
{
print "<h3>could not connect to database</h3>\n";
exit;
}
mysql_select_db("myusername");
?>
##Here's my code for display_topic.php page
Code: Select all
<? include("dbconnect.php"); ?>
<? include("functions.php"); ?>
<html>
<head>
<title>Display Topics</title>
<link rel="stylesheet" href="styles.htm" type="text/css">
</head>
<body bgcolor=white>
<p>
<a href="display_topic.php">topic list</a> |
<a href="write_topic.php">new topic</a>
<br>
<br>
</p>
<?
if (!isset($topic_ID)) { $topic_ID = 0; }
DisplayTopic($topic_ID);
?>
</body>
</html>
##Here's my code for write_topic.php
Code: Select all
<? include("functions.php"); ?>
<? include("dbconnect.php"); ?>
<html>
<head>
<title>Write Topics</title>
<link rel="stylesheet" href="styles.htm" type="text/css">
</head>
<body bgcolor=white>
<p>
<a href="display_topic.php">topic list</a> |
<a href="write_topic.php">new topic</a>
<br>
<br>
</p>
<?
if (!isset($Create) || $Create != "Create")
{
if (isset($topic_id))
{
list($root_ID, $parent_ID, $Name) = DisplayTopic($topic_id,0,1);
print "<p><hr><b>Care to comment?</b></p>\n";
}
else
{
$topic_ID = 0;
$root_ID = 0;
$Name = "";
}
?>
<form method=post action="write_topic.php">
<input type=hidden name="parent_ID" value="<? print $topic_ID; ?>">
<input type=hidden name="root_ID" value="<? print $root_ID; ?>">
</p>
<p>
Topic Subject: <input type=text name=name value="<?
print $topic_ID > 0 ? "Re: $Name" : "";
?>" size=50><br>
Your Name: <input type=text name=author size=50><br><br>
Your Words Here:
<br>
<textarea name=description cols=60 rows=10></textarea>
<br>
<input type=submit name=Create value="Create">
</p>
</form>
</body>
</html>
<?
}
else
{
$topic_ID = CreateTopic ($Name, $author, $Description
, $parent_ID, $root_ID
);
DisplayTopic($topic_ID);
}
?>
##And here's my code for functions.php
Code: Select all
function DisplayKids ($topic_ID=0, $level=0)
{
$comments = 0;
$query = "select topic_ID, Name, author "
.", create_dt, description from topics where "
." parent_ID = $topic_ID "
." order by create_dt, topic_ID "
;
#print "<p>$query</p>\n";
$result = mysql_query($query);
while (list($r_topic_ID, $r_Name, $r_author, $r_create_dt, $r_Description)
= mysql_fetch_row($result)
)
{
if (!$level)
{
if (!$comments)
{
print "<b>Comments:</b><br>\n";
}
print "<table border=0>\n"
."<tr bgcolor=skyblue><td colspan=2 width=500>\n";
}
else
{
if (!$comments)
{
print "<ul>\n";
}
print "<li>";
}
$comments++;
if (!eregi("[a-z0-9]",$r_author)) { $r_author = "[no name]"; }
if ($r_topic_id != $topic_ID)
{
?>
<a href="display_topic.php?topic_ID=<? print $r_topic_ID; ?>"><? print $r_Name; ?></a> by <b><? print $r_author; ?></b>
<?
}
else
{
?>
<? print $r_Name; ?> by <b><? print $r_author; ?></b>
<?
}
?>
( <? print $r_topic_ID; ?> )
<?
if ($level)
{
print "<br>\n";
}
else
{
print "</td></tr>\n"
."<tr><td width=2> </td>\n"
."<td width=498>$r_Description</td>\n"
."</tr></table><br>\n"
;
}
DisplayKids($r_topic_ID, $level+1);
}
if ($level && $comments)
{
print "</ul>\n";
}
}
function DisplayTopic ($topic_ID=0, $level=0, $nokids=0)
{
$fields = array("topic_ID", "parent_ID", "root_ID", "Name"
, "Description", "author", "author_host", "create_dt"
, "modify_dt"
);
$query = "select distinct ";
$comma = "";
while (list($key,$val)=each($fields))
{
$query .= $comma."t.".$val;
$comma = ", ";
}
if (!$topic_ID)
{
$query .= " from topics t , topics r "
."where t.topic_ID = r.root_ID "
;
#print "<p>$query</p>\n";
$result = mysql_query($query);
if (!$result)
{
print "<p>Damn! result = $result</p>\n";
}
else
{
while ($row = mysql_fetch_row($result))
{
list($topic_ID, $parent_ID, $root_ID, $Name
, $Description , $author, $author_host
, $create_dt, $modify_dt, $parent_name
, $root_name
) = $row;
print "<p>"
."<a href="display_topic.php?topic_ID="
.$topic_ID
."">"
.$Name
."</a></p>\n"
;
}
}
return array(0,0,"");
}
else
{
$query .= ",p.Name,r.Name "
."from topics t "
."left join topics as p on t.parent_ID = p.topic_ID "
."left join topics as r on t.root_ID = r.topic_ID "
."where t.topic_ID = $topic_ID "
;
#print "<p>$query</p>\n";
$result = mysql_query($query);
if (!$result)
{
print "<p>Damn! result = $result</p>\n";
}
else
{
list($topic_ID, $parent_ID, $root_ID, $Name
, $Description , $author, $author_host
, $create_dt, $modify_dt, $parent_name
, $root_name
) = mysql_fetch_row($result);
}
}
if ($author == "") { $author = "[no name]"; }
if ($root_ID != $topic_ID && $root_ID != $parent_ID)
{
print "<p>\n<b>root:</b>\n"
."<a href="display_topic.php?topic_ID="
.$root_ID
."">"
.$root_name
."</a>\n</p>\n"
;
}
if (isset($parent_name) && $parent_name > "")
{
print "<p>\n<b>parent:</b>\n"
."<a href="display_topic.php?topic_ID="
.$parent_ID
."">"
.$parent_name
."</a>\n</p>\n"
;
}
print "<p>\n"
."<b>$Name</b> by <b>$author</b> ($author_host) "
."on <b>$create_dt</b>\n"
."</p>\n"
."<p>\n"
.$Description
."</p>\n"
;
if (!$nokids)
{
print "<p><b>"
."<a href="write_topic.php?topic_ID=$topic_ID">"
."Reply to this</a></b></p>\n"
;
print "<p>\n";
DisplayKids($topic_ID, $level);
print "</p>\n";
}
return array($root_ID, $parent_ID, $Name);
}
function FixQuotes ($what = "")
{
$what = ereg_replace("'","''",$what);
$counter = 0;
while (eregi("\\\''", $what) && $counter < 10)
{
$what = ereg_replace("\\\''","'",$what);
#print "<p>counter=[$counter] what=[$what]</p>\n";
}
return $what;
}
function CreateTopic ($Name="[no name]", $author="[no author]"
, $Description="[no comments]", $parent_ID=0, $root_ID=0
)
{
$Name = FixQuotes($Name);
$Description = FixQuotes($Description);
$author = FixQuotes($author);
$query = "insert into topics "
." (Name,Description, parent_ID, root_ID "
." , author, author_host) "
." values ('$Name', '$Description', $parent_ID "
." , $root_ID, '$author', '".getenv("REMOTE_ADDR")."' "
.") "
;
print "<p>$query</p>\n";
$result = mysql_query($query);
if (!$result)
{
print "<p>insert failed.</p>\n";
}
$result = mysql_query("select last_insert_id()");
list($topic_ID) = mysql_fetch_array($result);
if (isset($topic_ID) && $topic_ID > 0)
{
#print "<p>topic_ID = [$topic_ID]</p>\n";
if ($root_ID == 0)
{
$root_ID = $topic_ID;
mysql_query("update topics set root_ID=$topic_ID "
." where topic_ID = $topic_ID and root_ID=0 "
);
}
}
else
{
print "<p><b>that didn't work.</p>\n";
}
return $topic_ID;
}