+ Reply to Thread
Results 1 to 8 of 8

Thread: Need to design a 'post comment page'

  1. #1
    imajinn1 is offline x10Hosting Member imajinn1 is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    47

    Need to design a 'post comment page'

    I started to work on a site and now the customer wants a post comments page.

    I can get my way around with simple PHP (like form to email) but not with the database setup.
    Does anyone know any 3rd party sofwares, links or leads i can use to design this page?

    Any kind of help is appreciated.
    Thanks

  2. #2
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: Need to design a 'post comment page'

    Writing it yourself is pretty simple.

    Just make a page with the form that the user will write what they want for their comment:
    HTML Code:
    <form action="postReview.php" method="post">
    <textarea cols="20" rows="5" name="comment"></textarea><br />
    <input type="text" name="poster" size="12" /><br />
    <input type="submit" value="Post comment." />
    </form>
    And make a page to add it to the database:
    PHP Code:
    <?php
    # Define values host,dbname,username and password as $s before the try{} statement

       
    try {
         
    $dbh = new PDO("mysql:host=$host;dbname=$dbname"$username,$password);
         
    $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
       } catch (
    PDOException $e) {
         print (
    "Could not connect to server.\n");
       }

    # Comments page

    # Get the info and organize it in an array
    $info = array(
    "com"=>$_POST['comment'],
    "name"=>$_POST['poster']);
    # Create an array in case of errors
    $errors = array();

    # Check each entry
    if(empty($info['com']))
          
    $errors[]="Invalid comment.";
    if(empty(
    $info['name']))
          
    $errors[]="Invalid username.";

    # If there were errors tell them what went wrong and post a link to go back to the form page
    if($errors) {
          echo 
    "There were errors with your post: ";
          echo 
    "<ul><li>";
          echo 
    implode("</li><li>",$errors);
          echo 
    "</li></ul>";
          echo 
    "<a href='formPage'>Go back</a>";
          exit;
    }

    # SQL query that will insert data into database
    $sth $dbh->prepare("INSERT INTO comments(id,comment,poster,date) VALUES(0,?,?,now())");

    $sth->execute(array($info['com'],$info['name']));

    echo 
    "Comment posted.";
    ?>
    And in your database (assuming you use the PHPMyAdmin) create the table and each column value (or just import this code (save as .sql file and click the Import tab))
    Code:
    CREATE TABLE comments(id int(255) PRIMARY KEY auto_increment, comment text, poster varchar(30), date varchar(100))
    And there you have it. Place the HTML on whichever page the customer wants it, and create a page (which I called 'postReview.php') for the PHP->mysql. But import the sql in first or the whole thing will not work.

    Hope this helps.
    Last edited by as4s1n; 04-22-2010 at 07:57 PM. Reason: Typo in PHP script
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  3. #3
    imajinn1 is offline x10Hosting Member imajinn1 is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    47

    Re: Need to design a 'post comment page'

    Yes it does help. It is already something i can test on my site

    Im gonna play around with what you sent me and make it work.
    i will give you a feedback on this.

    thank you very much

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Need to design a 'post comment page'

    Quote Originally Posted by as4s1n View Post
    PHP Code:
    <?php
    # Define values host,dbname,username and password as $s before the try{} statement

       
    try {
         
    $dbh = new PDO("mysql:host=$host;dbname=$dbname"$username,$password);
    Sensitive information such as user credentials shouldn't be scattered across multiple scripts; they should be isolated in a single script.

    Quote Originally Posted by as4s1n View Post
    PHP Code:
         $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
       } catch (
    PDOException $e) {
         print (
    "Could not connect to server.\n");
       }

    # Comments page

    # Get the info and organize it in an array
    $info = array(
    "com"=>$_POST['comment'],
    "name"=>$_POST['name']);
    [...]
    # SQL query that will insert data into database
    $sth $dbh->prepare("INSERT INTO comments(id,comment,poster,date) VALUES(0,?,?,now())");

    $sth->execute(array($info['com'],$info['name'])); 
    There's an unnecessary array here. You can either use:
    PHP Code:
    $info = array(
        
    ":com"=>$_POST['comment'],
        
    ":name"=>$_POST['name']
    );
    [...]
    $sth $dbh->prepare("INSERT INTO comments(id,comment,poster,date) VALUES(0,:com,:name,now())");
    $sth->execute($info); 
    or:
    PHP Code:
    $sth $dbh->prepare("INSERT INTO comments(id,comment,poster,date) VALUES(0,?,?,now())");
    $sth->execute(array($_POST['comment'], $_POST['name'])); 

    Lastly, don't forget to wrap a try {...} catch (PDOException $e) {...} around all the calls to PDO methods.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    imajinn1 is offline x10Hosting Member imajinn1 is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    47

    Re: Need to design a 'post comment page'

    thank to both of u for the reply.

    I am getting confused with the second set of scripts but i will get my way around in the coming days

  6. #6
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: Need to design a 'post comment page'

    If you didn't understand something, I highlighted all of misson's corrections.

    Sorry, those were a few imperfections, while not critical, it makes it easier to read and manage. First, although it was implied, you should put the database connection handler ($dbh) on the index or on a separate page that you reference via PHP::include() on the index page.

    Second, in the STH variable, I included the values of the array which you do not need to do.

    E.G.
    Code:
    $sth = $dbh->prepare("INSERT INTO comments(id,comment,poster,date) VALUES(0,?,?,now())"); 
    
    $sth->execute(array($info['com'],$info['name']));  
    When it should have been what misson wrote:

    Code:
    $sth = $dbh->prepare("INSERT INTO comments(id,comment,poster,date) VALUES(0,name,com,now())"); 
    $sth->execute($info);
    You do that because the value in the params for execute() needs to be an array and if the value is then you can reference each entry in the array by its key (I.E. 'name'=>$_POST['name']) and you can just call the value by its key inside the prepare (I.E. ...VALUES(0,name,com,0);

    Thirdly, the try/catch should have been around all the connections.

    Code:
    try{
    $sth = $dbh->prepare("INSERT INTO...");
    } catch(PDOException $e) {
    echo "Errors: ".$e->getMessage();
    Last edited by as4s1n; 04-23-2010 at 11:19 AM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  7. #7
    sikuneh is offline x10Hosting Member sikuneh is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    43

    Re: Need to design a 'post comment page'

    Wouldn't you only have to wrap a try/catch statement around the PDO::EXECUTE()? Because nothing does anything (assuming its a PREPARE) until the execute() is called.
    Last edited by sikuneh; 04-26-2010 at 01:09 PM.

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Need to design a 'post comment page'

    @sikuneh: PDO creation and PDO::prepare can also generate exceptions.
    Last edited by misson; 05-06-2010 at 04:33 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Replies: 5
    Last Post: 05-01-2009, 06:51 AM
  2. comment in my post for 20 credits.
    By TPI007 in forum The Marketplace
    Replies: 16
    Last Post: 09-01-2008, 12:18 PM
  3. Rate, Comment, and Review my page
    By renacac in forum Review My Site
    Replies: 4
    Last Post: 07-27-2008, 08:13 PM
  4. Rate and comment my 3-page website
    By MarioMaster in forum Review My Site
    Replies: 6
    Last Post: 07-27-2008, 09:02 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