Page 3 of 5 FirstFirst 12345 LastLast
Results 21 to 30 of 47

Thread: How to post comments to a php page

  1. #21
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    are you pointing the php code to your mysql server, or your homepage?
    correct:
    mysql_connect("http://mysql_install_path.domain.com/","user","password");
    or:
    mysql_connect("http://www.domain.com/mysql_install_path/","user","password");
    incorrect:
    mysql_connect("http://www.domain.com/","user","password");
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  2. #22
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Yes, I have the database, user and password correct. In fact, I created 2 mysql databases - one in 4.1 and the other 5.0 because I thought the error message was related to that but it was not.

    This is the page now. Where are the boxes to make a post? What am I doing wrong here? I think I am overlooking something simple.

    http://propheciesofrevelation.org/test.php

  3. #23
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    In my table called "comments", do I create 2 "fields" -- one called "name" and the other called "comments"?

    Do I only make one table for the one database? And then with two fields?

    Or are more tables and fields involved that I am not figuring out?

    Dre

  4. #24
    Join Date
    Aug 2007
    Location
    Ohio
    Posts
    79
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Default

    Okay, a database can only hold many tables but you will only need one. "comments" is a fine name for the table. If you want to collect both their name and a comment, you will need two fields in the table. One for the comment and one for the name.

    In order to get data from the user, you need to use html to place forms for them to use. Here's an example:

    Code:
    <form method="post" action="submit.php">
    <p>Name: <input type="text" name="name" /></p>
    <p>Comment:</p>
    <textarea rows="5" cols="40" name="comment"></textarea>
    <p><input type="submit" value="Post Comment" />
    </form>

    Then in submit.php, you would have to get that data and store it into the database:
    Code:
    <?php
    
    if(isset($_REQUEST['name']) && isset($_REQUEST['comment'])) {
    
    $conn = mysql_connect("localhost","user","password");
    mysql_select_db("database_name",$conn);
    
    mysql_query("INSERT INTO comments (name, comment) VALUES('".mysql_real_escape_string($_REQUEST['name'])."', '".mysql_real_escape_string($_REQUEST['comment'])."' ");
    
    echo "Thank you, your comment has been submitted.";
    
    }
    
    ?>
    Then you could use something similar to what I posted on page 2 to view the comments.

  5. The Following User Says Thank You to jackbenimble4 For This Useful Post:

    Benedizione (02-18-2008)

  6. #25
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Everything is almost working, but how do I get the comments to post back to the main page? What word do I need to replace here?

    This is my submit.php page. Isn't it something on here that I need to change? Comments.txt, perhaps?


    <?php
    if(isset($_POST['submit']) && $_POST['name'] != null && $_POST['comments'] != null) {
    $file=fopen("comments.txt","r+");
    $contents=fread($file,filesize("comments.txt")+1);
    $contents=$contents."\n[".$_POST['name']."]".$_POST['comments'];
    fwrite($file,$contents);
    fclose($file);
    }
    ?>

  7. #26
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    after it submits you want it to go to the main page?
    Code:
    <?php
    if(isset($_POST['submit']) && $_POST['name'] != null && $_POST['comments'] != null) {
    $file=fopen("comments.txt","r+");
    $contents=fread($file,filesize("comments.txt")+1);
    $contents=$contents."\n[".$_POST['name']."]".$_POST['comments'];
    fwrite($file,$contents);
    fclose($file);
    header("Location: http://www.yoursite.com/main_page.php");
    }
    ?>
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  8. #27
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by Benedizione View Post
    However, I am receiving very rude feedback from the tech support of dynamic drive. I may be forced to leave this forum altogether. Maybe they should send me to prison for having used "a font".
    Please don't take offense. People don't like when you over-use fonts/formatting (Based on your previous post, you probable made the font size bigger? Those things should usually be used sparingly). It's nothing personal--we want you to stay on DynamicDrive-- it's just a reminder to keep posts simple.

    BTW, do you mean pulling the comments back out of the database? That would take a MySQL Query.
    Code:
    $rows = MySQL_Query("SELECT * FROM comments");
    while($row = MySQL_Fetch_array($rows)){
    echo $row['comment'];
    }
    (that's a very basic example)

    Edit: If you want to display the results from newest to oldest, you need to add another column to your table, either a time of an ID to sort by.
    Last edited by Jas; 02-18-2008 at 09:00 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  9. #28
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Is that where the posts are going? To the database? I wondered where they were going. lol

    Okay, the mysql query code goes on which page? The test.php or submit.php? Seems like the code should be placed on the test.php page....placed on the page where I want the posts to appear. Would that be correct?

    By "another column to your table" do you mean "field"? Create another field and call it "time" or "ID"?

  10. #29
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Quote Originally Posted by Benedizione View Post
    Is that where the posts are going? To the database? I wondered where they were going. lol
    Are you using the code you posted here:

    Code:
    <?php
    if(isset($_POST['submit']) && $_POST['name'] != null && $_POST['comments'] != null) {
    $file=fopen("comments.txt","r+");
    $contents=fread($file,filesize("comments.txt")+1);
    $contents=$contents."\n[".$_POST['name']."]".$_POST['comments'];
    fwrite($file,$contents);
    fclose($file);
    header("Location: http://www.yoursite.com/main_page.php");
    }
    ?>
    If so, then they are being written to the file comments.txt. In order to read that file, you would want to put the following on the page that you want the comments to be displayed.

    Code:
    <?php
     foreach (file('comments.txt') as $comment) {
       echo $comment.'<br><hr><br>';
     }
    ?>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  11. #30
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Yes, that is the code I am using.

    Place this code onto my main page where I want the comments to appear?

    <?php
    foreach (file('comments.txt') as $comment) {
    echo $comment.'<br><hr><br>';
    }
    ?>


    I have been playing around with it different ways. When I place it on the page where I want the comments to appear, then the page is messed up and this link shows up "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ".

    My experiment page is here http://www.propheciesofrevelation.org/test.php

    I am not planning to keep the page test.php. I am trying to experiment with that page as a main page so that I do not interfere with the main website. When I get this code figured out then I will integrate it into the main site on the appropriate page.

    Dre

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •