Page 1 of 1

I have a BIG problem..

Posted: Sun Feb 02, 2003 8:50 pm
by hannasu
Okay, the thing is that I don't know a lot about PHP, but I have this project at school that I need to have ready in 2 days time. I need to make a php/MySQL using discussion forum. I found a tutorial for it from this url: http://hotwired.lycos.com/webmonkey/tem ... _meta.html but there is one link that doesn't work, I mean, it should have "modelcodes" for four files: display_topic.phtml, write_topic.phtml, dbconnect.phtml, and functions.phtml, the dbconnect.phtml link doesn't work. And I have like NO IDEA what to write in it. Can anyone of you study the other links and let me know what to do? I'm a mess because of this, it's now almost 5AM and I've been trying to do everything that I know to this file, but nothing happens :cry: ! I'd be sooooo grateful if you could help, and then I could also graduate... :o . Thank you already guys!

Posted: Sun Feb 02, 2003 9:27 pm
by volka
http://extranet.hotwired.com:8080/mysql/dbconnect.phtmls: the link works and shows
<p><a href="/brad/php/">PHP Examples</a><br><br></p>

<?
if (!($mylink = mysql_connect("localhost","nobody","")))
{
print "<h3>could not connect to database</h3>\n";
exit;
}
mysql_select_db("test");
?>

Posted: Sun Feb 02, 2003 9:50 pm
by hannasu
Well the link shows that yeah, but that CAN'T be the right code, right? Cos I sure can't make that work. So is it the code or am I just stupid? Argh, I'm getting desperate here :cry:

Posted: Sun Feb 02, 2003 10:50 pm
by Stoker
I don't see anything wrong with that code.. ofcourse you have to set the correct user and password, and did you create a database?

Some info on what errors you get etc is always useful..

Here's my code

Posted: Mon Feb 03, 2003 6:16 am
by hannasu
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;
}

Posted: Mon Feb 03, 2003 6:26 am
by twigletmac
So you don't get any other error messages at all? It could be something to do with how you are refering to the information sent from the form, have you read this:
viewtopic.php?t=511

Mac

Posted: Mon Feb 03, 2003 6:39 am
by volka
please read viewtopic.php?t=511
Depending on the version of php you're using this may fix a lot.
e.g.

Code: Select all

<?php ...
if (!isset($_POST['Create']) || $_POST['Create'] != "Create")
... ?>
The example code uses <?... ?> to open/close php-blocks. This only works if short-tags are enabled. Try it with <?php ... ?> instead.

Check wether each block has matching php-tags, e.g. the snippet of dbconnect.php you posted has no opening <?php (but the ?> is ok)

To track errors it is helpful to increase the error reporting level and (since this is a simple example) to make errors displayed within the browser.
In display_topic.php and write_topic.php insert at the beginning

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
?>

Okay, MORE errors..

Posted: Mon Feb 03, 2003 10:22 am
by hannasu
Thanks for your help, although I don't know what I'm doing wrong :cry: . I did what Volka suggested and put on the error reports. I got these back..

Fatal error: Call to undefined function: createtopic() in /home/sites/haunted-heart.net/write_topic.php on line 57

and

Parse error: parse error in /home/sites/haunted-heart.net/functions.php on line 105

PLEASE if someone could check those files, they're at [url]http://cgi.haunted-heart.net/display_topics.php[/url]

I feel so damn stupid. lol.

Posted: Mon Feb 03, 2003 11:50 am
by bznutz
Holy crap. You are going about this all wrong homie. You are trying to run around and put out fires as they come up. You need to do this top-down. Let's attack the first problem: the database.
You have to make sure that:
A. You can connect to it.
B. The username/pass you connect with are for an account with read/write
privileges to the database you are using.

Perhaps we can take you through step by step if you give us some background info. What operating system is this forum running on? Is it your machine or someone else's?