By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

php and POST form not acting normal...

P: n/a
Hi All,

I have recently built a site using PHP and MySQL, and started to
implement a basic forum into it. However, the form to post messages
doesnt do what I want it to. If there is no user logged in (ie no
session) then it posts the message fine. But if there is a user logged
in, it loads my login.php page instead of my post.php page??? Code for
both pages below:
>>>>>READ.PHP>>>>>>>>>
<?php
session_start();
include("mngmnt.inc");
connect_to_db();
@$threadid=$_GET['thread'];
$_SESSION['page']="Forum - Read/Post";

print_header();
print_menu();

print "<form method=\"POST\" action=\"post.php\">\n
<input name=\"thrid\" type=\"hidden\" value=\"".$threadid."\">\n";
if($threadid=="new")
{
print "<center><table width=\"400\"><tr><th width=\"*\"
align=\"left\">Post:</th></tr>\n
<tr><td width=\"100%\" align=\"center\">Subject: <input
name=\"thread\" type=\"text\" size=\"60\"></td></tr>\n";
}
else
{
$threaddata=mysql_query("SELECT created, date, starter, username,
area, thread, body FROM forum WHERE tid='".$threadid."' ORDER BY
created LIMIT 1");
while($row=mysql_fetch_array($threaddata))
{
extract($row);
print "<a href=\"forum.php?area=$area\">$area</a> - <b>$thread</b>
(Started by ";
contact($starter);
print ")<p>\n";
}
$threaddata=mysql_query("SELECT created, date, username, body FROM
forum WHERE tid='".$threadid."' ORDER BY created");
while($row=mysql_fetch_array($threaddata))
{
extract($row);
print "<table width=\"100%\" columns=\"2\"><tr><th width=\"*\"
align=\"left\">By ";
contact($username);
print "</th><th width=\"225\" align=\"right\">Posted on ";
mysql_timestamp_to_human_basic($created);
print "</th></tr>\n
<tr><td align=\"left\">$body</td><td align=\"right\"
valign=\"bottom\">Last edited: ";
mysql_timestamp_to_human_basic($date);
print "</td></tr></table><p>\n<center><table width=\"400\"><tr><th
width=\"*\" align=\"left\">Reply:</th></tr>\n";
}
}

if(isset($_SESSION['username']))
{
print "<tr><td width=\"100%\" align=\"center\"><textarea
name=\"postbody\" cols=\"50\" rows=\"10\"></textarea></td></tr>\n
<tr><td align=\"center\"><input name=\"postsubmit\" type=\"submit\"
value=\"Post\"><input type=\"reset\"
value=\"Clear\"></td></tr></table>\n
</form>\n
</center>\n";
}
else
print "Login to reply</center>";

print_menu_right();
print_footer();
?>
>>>POST.PHP>>>>>>>>>
<?php
session_start();
include("mngmnt.inc");
connect_to_db();
@$thrid=$_POST['thrid'];
@$postbody=$_POST['postbody'];
@$logpost=$_POST['postsubmit'];

if($logpost=="Post")
{
$ts=timestamp_to_mysql();
if($thrid=="new")
{
}
else
{
$threaddata=mysql_query("SELECT starter, aid, area, thread FROM forum
WHERE tid='".$thrid."' LIMIT 1");
while($row1=mysql_fetch_array($threaddata))
{
extract($row1);
$putpost=mysql_query("INSERT INTO forum(created, date, starter,
username, aid, area, tid, thread, body) values(".$ts.", ".$ts.",
'".$starter."', '".$_SESSION['username']."', ".$aid.", '".$area."',
".$thrid.", '".$thread."', '".$postbody."')") or die(mysql_error());
if(!$putpost)
print "ERROR PUTTING POST";
else
print "<html>\n<head>\n<meta http-equiv=\"Refresh\" content=\"10;
url=read.php?thread=$tid\">\n</head>\n<body>\nPost Complete. Now
returning to the thread, please wait...\n</body>\n</html>\n";
}
}
}
mysql_close();
?>
>>>>>>>>>>>>


Any ideas?

TIA.

Alec.

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Alec wrote:
I have recently built a site using PHP and MySQL, and started to
implement a basic forum into it. However, the form to post messages
doesnt do what I want it to. If there is no user logged in (ie no
session) then it posts the message fine. But if there is a user logged
in, it loads my login.php page instead of my post.php page??? Code for
both pages below:
<snip unindented code>
Any ideas?


Yes.
1. Indent your code.

2. Remove *all* error-suppressing '@'s from it.

3. Add the following lines to the top of your scripts:
error_reporting(E_ALL);
ini_set('display_errors', '1');

4. *always* check the return value of mysql_query() calls
$sql = "select a, b, c from table where id=4";
$x = mysql_query($sql)
or die("Error in query [$sql]: " . mysql_error());
Apart from this, if something is behaving the opposite way you intended,
maybe reversing the if() will have the desired effect.

if(isset($_SESSION[úsername'])) /* your version */
if(!isset($_SESSION[úsername'])) /* reversed version */
--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.