+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: [PHP]: Having a problem with fwrite()

  1. #1
    AttackBunnyPro is offline x10Hosting Member AttackBunnyPro is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    26

    [PHP]: Having a problem with fwrite()

    PHP Code:
    <?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 by AttackBunnyPro; 10-11-2008 at 01:36 AM.

  2. #2
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: [PHP]: Having a problem with fwrite()

    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
    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>';
    Or try using nowdocs or heredocs (<<<EOT or <<<EOD)
    Last edited by natsuki; 10-11-2008 at 02:40 AM.

  3. #3
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: [PHP]: Having a problem with fwrite()

    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 Code:
    <?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");
    ?>
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  4. #4
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [PHP]: Having a problem with fwrite()

    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 Code:
    <?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 by xPlozion; 10-11-2008 at 09:12 AM.

  5. #5
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: [PHP]: Having a problem with fwrite()

    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 Code:
    <?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 by natsuki; 10-11-2008 at 12:39 PM.

  6. #6
    AttackBunnyPro is offline x10Hosting Member AttackBunnyPro is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    26

    Re: [PHP]: Having a problem with fwrite()

    Thanks, Marshian and Natsuki. Those did it.

  7. #7
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [PHP]: Having a problem with fwrite()

    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 by xPlozion; 10-11-2008 at 01:19 PM. Reason: Forums glitched up a bit (double posted when i hit it once o.O)

  8. #8
    AttackBunnyPro is offline x10Hosting Member AttackBunnyPro is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    26

    Re: [PHP]: Having a problem with fwrite()

    Hmph. Now my problem is getting it to write to a specific directory. How would I do this?

  9. #9
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: [PHP]: Having a problem with fwrite()

    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.

  10. #10
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [PHP]: Having a problem with fwrite()

    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 by xPlozion; 10-11-2008 at 03:18 PM.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Another Problem
    By beast474 in forum Free Hosting
    Replies: 1
    Last Post: 08-27-2008, 07:22 PM
  2. Slight Problem
    By rmb1993 in forum Free Hosting
    Replies: 2
    Last Post: 08-14-2008, 06:04 AM
  3. A problem with certificate
    By eon01 in forum Free Hosting
    Replies: 1
    Last Post: 07-31-2008, 01:27 PM
  4. DB number problem
    By lionheart8 in forum Free Hosting
    Replies: 5
    Last Post: 04-08-2008, 08:26 AM
  5. Ad code problem!
    By Akkarin in forum Free Hosting
    Replies: 8
    Last Post: 08-29-2005, 10:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers