[PHP]: Having a problem with fwrite()

AttackBunnyPro

New Member
Messages
26
Reaction score
0
Points
0
PHP:
<?php

$forumName = $_POST['forumName'];

if(!$forumName){
echo "fail";
}

mysql_connect('localhost','username','password');
mysql_select_db('abp_main');

mysql_query("INSERT INTO forum_forums
VALUES(NULL, '$forumName')");
$affectedRows = mysql_affected_rows();
$query1 = mysql_query("SELECT * FROM forum_forums
WHERE forumName='$forumName'");

$forumData = mysql_fetch_array($query1);
/***************************************************************************************************
****************************************************************************************************/
$forumID = $forumData['forumID'];
$fo=fopen($forumID."/index.php","w");

$fileData='<html>
<head>
<title>'.$forumName.' forum on ABP</title>
</head>
<body>
<table>
<?php

mysql_connect("localhost","abp_phpacc","q!4PTy@&8");
mysql_select_db("abp_main");

$sql = "SELECT * FROM forum_threads
WHERE forumID='$forumID'");
$query2 = mysql_query($sql);

while($results = mysql_fetch_array($query)){
echo "<tr><td><a href='".$forumID."/".$results["threadID"]."'>".$results["threadName"]."</a></td><td>Created by ".$results["threadCreator"]."</td></tr>";
}
?>
</table>
</body>
</html>';
fwrite($fo,$fileData);
fclose();
header("Location: index.php");
?>

I try to do that, but when I do, I get this error:

Parse error: syntax error, unexpected T_VARIABLE in /home/abp/public_html/test/admin/addForum.php on line 36
 
Last edited:

natsuki

New Member
Messages
112
Reaction score
0
Points
0
your $fileData got too complicated, try to echo it first so you know it really contains what you wanted, fwrite() isn't even the problem here
you have some problems with your strings there, the best way to do it might be first making the php page with the code, then copypaste it in your code, escaping the quotes that you need escaped.
If you are not sure, then try echo htmlspecialchars($fileData); or exit(htmlspecialchars($fileData)); before fwrite() so you'd see if it's showing what you wanted it to.

EDIT: this piece of code is erroneous:
Code:
$sql = "SELECT * FROM forum_threads
[color=red]WHERE forumID='$forumID'");[/color]
$query2 = mysql_query($sql);

while($results = mysql_fetch_array($query)){
[color=red]echo "<tr><td><a href='".$forumID."/".$results["threadID"]."'>".$results["threadName"]."</a></td><td>Created by ".$results["threadCreator"]."</td></tr>";[/color]
}
?>
</table>
</body>
</html>';

Or try using nowdocs or heredocs (<<<EOT or <<<EOD)
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
Just a syntax problem, you forgot to backslash the single quotes in the declaration of $fileData. Assuming all single quotes in the declaration were ment to be escaped, this is the code:
PHP:
<?php

$forumName = $_POST['forumName'];

if(!$forumName){
echo "fail";
}

mysql_connect('localhost','username','password');
mysql_select_db('abp_main');

mysql_query("INSERT INTO forum_forums
VALUES(NULL, '$forumName')");
$affectedRows = mysql_affected_rows();
$query1 = mysql_query("SELECT * FROM forum_forums
WHERE forumName='$forumName'");

$forumData = mysql_fetch_array($query1);
/***************************************************************************************************
****************************************************************************************************/
$forumID = $forumData['forumID'];
$fo=fopen($forumID."/index.php","w");

$fileData='<html>
<head>
<title>'.$forumName.' forum on ABP</title>
</head>
<body>
<table>
<?php

mysql_connect("localhost","abp_phpacc","q!4PTy@&8");
mysql_select_db("abp_main");

$sql = "SELECT * FROM forum_threads
WHERE forumID=\'$forumID\'");
$query2 = mysql_query($sql);

while($results = mysql_fetch_array($query)){
echo "<tr><td><a href=\'".$forumID."/".$results["threadID"]."\'>".$results["threadName"]."</a></td><td>Created by ".$results["threadCreator"]."</td></tr>";
}
?>
</table>
</body>
</html>';
fwrite($fo,$fileData);
fclose();
header("Location: index.php");
?>
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
Parse error: syntax error, unexpected T_VARIABLE in /home/abp/public_html/test/admin/addForum.php on line 36
This generally means that you forgot to put a semi-colon ( ; ) at the end of a line above the line with the problem. For example, it would be at the end of line 35 in this code.

The error codes in php are sorta self-explanatory once you get used to it and see them a few times. It's when they don't have any errors that you have to dig deeper :(

PHP:
<?php

$forumName = $_POST['forumName'];

if(!$forumName)
  echo "fail";

mysql_connect('localhost','username','password');
mysql_select_db('abp_main');

// if your first field was forumID, and the second was forumName
// in the original query, then you can use the one below.  if it's different
// then uncomment the second one and remove the top
mysql_query('INSERT INTO forum_forums (forumName) VALUES(\''.$forumName.'\')');
// mysql_query("INSERT INTO forum_forums VALUES(NULL, '$forumName')");
$affectedRows = mysql_affected_rows();
$query1 = mysql_query("SELECT forumID FROM forum_forums WHERE forumName='$forumName'");

$forumData = mysql_fetch_assoc($query1);


$forumID = $forumData['forumID'];
$fo=fopen($forumID."/index.php","w");

$fileData = '<html>
<head>
<title>'.$forumName.' forum on ABP</title>
</head>
<body>
<table>';
/*<?php*/ // you already have php tags opened xD

/* 
*  you don't need to connect to the database again since you have already done so above.  regardless if you open a new set
*  of tags, the db connection stays open until the script is done executing, then it's closed, or you explicitly closed it yourself 
*  by mysql_close.  you can remove the commented code below.
*/
// mysql_connect("localhost","abp_phpacc","q!4PTy@&8");
// mysql_select_db("abp_main");

$query2 = mysql_query("SELECT threadID, threadName, threadCreator FROM forum_threads WHERE forumID='$forumID'");

while($results = mysql_fetch_assoc($query))
{
  // .= means to addon to the string.  be careful though, if you dont have a string already defined, it will print a warning
  $fileData .= "<tr><td><a href='$forumID/{$results['threadID']}'>{$results['threadName']}</a></td><td>Created by {$results['threadCreator']}</td></tr>";
  // or
  // $fileData .= '<tr><td><a href="'.$forumID.'/'.$results['threadID'].'">'.$results['threadName'].'</a></td><td>Created by '.$results['threadCreator'].'</td></tr>';
  // whichever format you prefer.
}
/*?>*/ // same thing... it's okay, you're still learning :)
$fileData .= '</table>
</body>
</html>';

fwrite($fo,$fileData);
fclose();

header("Location: index.php");
?>

that should work

-xP
 
Last edited:

natsuki

New Member
Messages
112
Reaction score
0
Points
0
It's probable that the second db connection was to be done on the newly written file, that's why it was in the middle of a <table> the easiest method to deal with complicated strings with lots of quotes is to use the Heredocs syntax and escaping all variables that are not to be parsed now. It is as if you are writing the actual code: (Nowdocs would have been better if you don't need to parse any var then you won't need to escape a thing.)

PHP:
<?php ....
forumID = $forumData['forumID'];
$fo=fopen($forumID."/index.php","w");

$fileData = <<<EOT
<html>
<head>
<title>$forumName forum on ABP</title>
</head>
<body>
<table>
<?php

mysql_connect("localhost","abp_phpacc","q!4PTy@&8");
mysql_select_db("abp_main");

\$sql = "SELECT * FROM forum_threads
WHERE forumID='$forumID'";
\$query2 = mysql_query(\$sql);

while(\$results = mysql_fetch_array(\$query)){
echo "<tr><td><a href='$forumID/" . \$results["threadID"]."'>".\$results["threadName"]."</a></td><td>Created by ".\$results["threadCreator"]."</td></tr>";
}
?>
</table>
</body>
</html>
EOT;
fwrite($fo,$fileData);
fclose();
header("Location: index.php");
?>
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
hmm, never thought of it like that, I was under the assumption that he wanted a static page written from this xD

happy you got it working though...
 
Last edited:

natsuki

New Member
Messages
112
Reaction score
0
Points
0
you should know the directory and also make sure that the permissions in the said directory allows you to write to it maybe chmod 666 or 777. And also allow_url_fopen should be set so you can open from another location.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
Also, you can't write the file to a directory that doesn't exist ;). I had that problem with my code, so i added mkdir('/home/location/to/dir'); above my fopen.
 
Last edited:

AttackBunnyPro

New Member
Messages
26
Reaction score
0
Points
0
Yeah, after a while I figured out that it couldn't write to the directory, because it couldn't exist, and solved the problem. :p

Thanks anyway!

Please close this thread.
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
i'm pretty sure you ops can close their own threads in this forum. make a reply, but go into advanced mode. iirc, at the bottom where the check boxes are, theres a close thread checkbox.

-xP
 
Top