SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 39
  1. #1
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with INSERT comments

    Hey guys how are you?
    the comments table goes as follows

    CREATE TABLE IF NOT EXISTS `comments` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `shoename_id` int(11) NOT NULL,
    `created` datetime NOT NULL,
    `author` varchar(255) NOT NULL,
    `pros` text NOT NULL,
    `cons` text NOT NULL,
    PRIMARY KEY (`id`),
    KEY `appetizers_id` (`appetizers_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
    I need some help with inserting a comment in a database WHERE id=shoe_id

    This is the form format table

    HTML Code:
    <tr><td><table width="328" style="top:850px;" class="calamar"><td width="320" style=" line-height:3;"><strong>Comments:</strong></td>
    
    <tr></tr><td style="font-family:\'Times New Roman\', Times, serif; font-size:14px;">
          <form name="itemcomments" action="itemscomments.php" method="post">
            <label for="fullname"></label>
            <table class="delo" width="85%" border="0" cellspacing="6">
      <tr>
        <td width="15%"><label for="label">Nickname:</label>
        </td>
        <td width="85%"><input name="fullname" type="text" id="fullname" size="35" maxlength="85" /></td>
      </tr>
      <tr>
        <td>Pros:</td>
        <td><textarea name="textarea" cols="35" rows="5"></textarea></td>
      </tr>
      <tr>
        <td>Cons:</td>
        <td><textarea name="textarea" cols="35" rows="5"></textarea></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    
    
          </form>
          
    </table>
    </td>
    </tr>

    Later on I will ask you for some update, display of the comment and delete but by now I just need some example of inserting the comment.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean update? You would not use a where clause with an insert statement

  3. #3
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to insert a comment to the database by shoename and I want to display the comments by id. depending on the id then comments are going to be displayed.

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    //connect to mysql here

    //escape and secure data
    $comment mysql_real_escape_string($comment);
    $author mysql_real_escape_string($author);
    $pros mysql_real_escape_string($pros);
    $cons mysql_real_escape_string($cons);
    $shoe_id = (int)$shoe_id;

    $query "INSERT INTO `comments` 
    (shoename_id, created, author, pros, cons) 
    VALUES
    (
    $shoe_id, NOW(), '$author', '$pros', '$cons')";

    if(
    mysql_query($query)) {
        echo 
    "Thanks for your comment";
    }
    else {
        echo 
    "Unable to add your comment";
        
    //log mysql_error() here


  5. #5
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $comment 
    mysql_real_escape_string($comment);
    $author mysql_real_escape_string($author);
    $pros mysql_real_escape_string($pros);
    $cons mysql_real_escape_string($cons);
    $shoe_id $_GET (int)['id']; // This id will come from the url and I am going to $_GET is that scripting right?

    $query "INSERT INTO `comments` 
    ( created, author, pros, cons) 
    VALUES
    (
    $shoe_id, NOW(), '$author', '$pros', '$cons')";

    if(
    mysql_query($query)) {
        echo 
    "Thanks for your comment";
    }
    else {
        echo 
    "Unable to add your comment";
        
    //log mysql_error() here


    ?>
    <tr><td><table width="328" style="top:850px;" class="calamar"><td width="320" style=" line-height:3;"><strong>Comments:</strong></td>

    <tr></tr><td style="font-family:\'Times New Roman\', Times, serif; font-size:14px;">
          <form name="itemcomments" action="itemdetails2.php?id=<?php echo $shoe_id;?> method="post">
            <label for="fullname"></label>
            <table class="delo" width="85%" border="0" cellspacing="6">
      <tr>
        <td width="15%"><label for="label">Nickname:</label>
        </td>
        <td width="85%"><input name="fullname" type="text" id="fullname" size="35" maxlength="85" /></td>
      </tr>
      <tr>
        <td>Pros:</td>
        <td><textarea name="textarea" cols="35" rows="5" ><?php echo $pros?></textarea></td>
      </tr>
      <tr>
        <td>Cons:</td>
        <td><textarea name="textarea" cols="35" rows="5"><?php echo $cons?></textarea></td>
      </tr>

       <tr>
          <td></td>
          <td colspan="2"><input type="reset" value="Reset Fields">     
            <input type="submit" name="submit" value="Add Comment"></td>
        </tr>
    </table>


          </form>
          
    </table>
    </td>
    </tr>
    I came up with the form and the insert script you gave me please correct me any errors it could have. Right now is not working please help...

  6. #6
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to close your form action attribute. Right now it has no closing quote, so it runs on. After that you'll need to put the PHP code you have there in itemdetails2.php

    Actually your form is quite broken. The name of the text area should probably be pros (and another one for cons).
    Similarly <label for="label">Nickname</label> doesn't make much sense. The for attribute of the label tag should match the ID of the related form field.
    So you should have a text input called author, with ID of author and the label should then be <label for="author">Nickname</label>

    Validating your HTML will help spot a lot of these issues.

    The PHP script will then access these through the $_POST array:
    PHP Code:
    $author mysql_real_escape_string($_POST['author']); //POST key matches field name 
    And the shoe_id should be like this:
    PHP Code:
    $shoe_id = (int)$_GET['shoe_id']; 

  7. #7
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    After that you'll need to put the PHP code you have there in itemdetails2.php
    Which part of the php code will have to put it in itemdetails2.php

    --The form is in the same itemdetails2.php and it will display the comments on top of the form in the same itemdetails2.php is that possible?

  8. #8
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, it can be in the same script, but you'll need to detect when the form is submitting (as this page will also be viewed without a form submission)
    PHP Code:
    if(isset($_POST['name_of_form_button'])) {

      
    //add comment to DB
      //as per code above



  9. #9
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    if(isset($_POST['Add Comment ' ])){ // this is the name of the summit button in the form 

      //add comment to DB
      //as per code above
    $comment mysql_real_escape_string($comment);
    $author mysql_real_escape_string($author);
    $pros mysql_real_escape_string($pros);
    $cons mysql_real_escape_string($cons);
    $shoe_id $_GET (int)['id']; // This id will come from the url and I am going to $_GET is that scripting right?

    $query "INSERT INTO `comments` 
    ( created, author, pros, cons) 
    VALUES
    (
    $shoe_id, NOW(), '$author', '$pros', '$cons')";

    if(
    mysql_query($query)) {
        echo 
    "Thanks for your comment";
    }
    else {
        echo 
    "Unable to add your comment";
        
    //log mysql_error() here


    }

    ?>
    is it something like this?

  10. #10
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Actually your form is quite broken. The name of the text area should probably be pros (and another one for cons).
    HTML Code:
    <tr><td><table width="328" style="top:850px;" class="calamar"><td width="320" style=" line-height:3;"><strong>Comments:</strong></td>
    
    <tr></tr><td style="font-family:\'Times New Roman\', Times, serif; font-size:14px;">
          <form name="itemcomments" action="itemdetails2.php?id=<?php echo $shoe_id;?> method="post">
            <label for="fullname"></label>
            <table class="delo" width="85%" border="0" cellspacing="6">
      <tr>
        <td width="15%"><label for="label">Nickname:</label>
        </td>
        <td width="85%"><input name="fullname" type="text" id="fullname" size="35" maxlength="85" /></td>
      </tr>
      <tr>
        <td>Pros:</td>
        <td><textarea name="textarea" cols="35" rows="5" ><?php echo $pros; ?></textarea></td>
      </tr>
      <tr>
        <td>Cons:</td>
        <td><textarea name="textarea" cols="35" rows="5"><?php echo $cons; ?></textarea></td>
      </tr>
    
       <tr>
          <td></td>
          <td colspan="2"><input type="reset" value="Reset Fields">     
            <input type="submit" name="submit" value="Add Comment"></td>
        </tr>
    </table>
          </form>
          
    </table>
    </td>
    </tr>
    I have both textarea in the form

    Pros
    PHP Code:
    <tr>
        <td>Pros:</td>
        <td><textarea name="textarea" cols="35" rows="5" ><?php echo $pros?></textarea></td>
      </tr>
    Cons
    PHP Code:
      <tr>
        <td>Cons:</td>
        <td><textarea name="textarea" cols="35" rows="5"><?php echo $cons?></textarea></td>
      </tr>
    Did you meant something else?

    You also mentioned that

    Quote Originally Posted by cranial-bore View Post
    Similarly <label for="label">Nickname</label> doesn't make much sense. The for attribute of the label tag should match the ID of the related form field.
    So you should have a text input called author, with ID of author and the label should then be <label for="author">Nickname</label>
    Label nick name does't make much sense how would I code it better and more understandable.

    When you said the for attribute of the label tag should match the ID of the related form field. Where can I find the related form field.

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like you want to have two fields, one for Nickname and one for Full Name, but your database cannot handle this.

    Do you want a field for both? Or just one?

    e39m5

  12. #12
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,287
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    Code:
          <form name="itemcomments" action="itemdetails2.php?id=<?php echo $shoe_id;?>" method="post"> did you see how I ninja'd that " in there?
           <label for="fullname"></label> <-- what this doing???
            <table class="delo" width="85%" border="0" cellspacing="6">
      <tr>
        <td width="15%"><label for="fullname">Nickname:</label>
        </td>
        <td width="85%"><input name="fullname" type="text" id="fullname" size="35" maxlength="85" /></td>
      </tr>
      <tr>
        <td>Pros:</td>
        <td><textarea name="textarea" cols="35" rows="5" ><?php echo $pros; ?></textarea></td>
      </tr>
      <tr>
        <td>Cons:</td>
        <td><textarea name="textarea" cols="35" rows="5"><?php echo $cons; ?></textarea></td>
      </tr>
    
       <tr>
          <td></td>
          <td colspan="2"><input type="reset" value="Reset Fields">     
            <input type="submit" name="submit" value="Add Comment"></td>
        </tr>
    </table>
          </form>
    Though of course it would be gorgeous like this:
    Code:
    <form id="itemcomments" action="itemdetails2.php?id=12345678" method="post">
      <fieldset>
        <legend>Make a comment on this item</legend>
        <div>
          <label for="nickname">Nickname:</label>
          <input type="text" name="nickname" id="nickname" maxlength="85" />
        </div>
        <div>
          <label for="fullname">Full Name:</label>
          <input type="text" name="fullname" id="fullname" maxlength="85" />
        </div>
        <div>
          <label for="pros">Pros:</label>
          <textarea name="pros" id="pros" cols="35" rows="5"></textarea>
        </div>
        <div>
          <label for="cons">Cons:</label>
          <textarea name="cons" id="cons" cols="35" rows="5"></textarea>
        </div>  
        <input type="submit" name="submit" value="Add Comment">
        <input type="reset" value="Reset Fields">   
      </fieldset>
    </form>
    This is assuming you really wanted both a real name and a nickname, so I have two different questions. Maybe you're swapping one for the other. NOt sure.

    Removed the size attribute though it's ok to have, but you know, you can CSS that : )

    You have XHTML endings on your inputs, so I know you shouldn't have the name attribute in your form (no name="blah" on the form itself, it's still ok for form controls inside the form). No, I still don't know how to make a backend script know which form is sending what (there's some way of doing it with id but I dunno what exactly, so I changed your name into an id).

    The divs aren't necessary but can make styling the form easier.

    I always thought it made more sense to put the submit before the reset. I have this nasty habit of filling out a form and then hitting Enter without first selecting the button. Sometimes I think IE got this one right and we should HAVE to select a button with the focus or something before hitting Enter...

    Every label gets a for that matches its input's id, as cranial-bore said. Run your HTML (after the browser gets it, not with the PHP in it) to the validator.
    Last edited by Stomme poes; Jul 9, 2009 at 06:52. Reason: changed name of textareas

  13. #13
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your form field names are how you identify those values in the $_POST array.
    They should not just be a repeat of the field type.

    e.g
    Code HTML:
    <!-- GOOD -->
    <input type="text" name="nickname" id="nickname">
    <textarea name="pros" rows="6" cols="50"></textarea>
     
    <!-- BAD -->
    <input type="text" name="text">
    <textarea name="textarea">

    The <label> tag should link to the appropriate form field. You look at the id attribute of the form field and put that same value in the for attribute of the label tag.

    PHP Code:
    <?php

    if(isset($_POST['Add Comment ' ])){ // this is the name of the summit button in the form 

      //add comment to DB
      //as per code above
    $comment mysql_real_escape_string($comment);
    Add Comment is the value not the name of your button. See your HTML code. The value is just a user friendly piece of text, the name is what matters. You also can't just add extra spaces inside the array key (you have a space after Comment)

    PHP Code:
    /*
    You need to get these variables from POST
    they won't automatically exist
    you need to reference them by the NAME of their
    input tag
    */
    $comment mysql_real_escape_string($_POST['comment']); 

  14. #14
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,287
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by co.ador
    When you said the for attribute of the label tag should match the ID of the related form field. Where can I find the related form field.
    In case this is still a question: "the related for field" is the input who your label references. So
    Nickname: _______________ <-- that's the nickname label's related form field, the text input. For the textareas, you didn't have a label (and should have), so right now with your old table code, you have a form field (textarea) without its label. Twice.

  15. #15
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    [highlight=HTML]
    <!-- GOOD -->
    <input type="text" name="nickname" id="nickname">
    <textarea name="pros" rows="6" cols="50"></textarea>

    The <label> tag should link to the appropriate form field. You look at the id attribute of the form field and put that same value in the for attribute of the label tag.
    You don't actually need the name attribute, unless the script needs that. For form controls, id's needs to be given. this is what $_POST & $_GET pick up. Then as Mike said the id is what is used for the for="" attribute. What this does is directly link whatever is between the label tags to the form element. This allows people using assistive technologies to use the form
    Ryan B | My Blog | Twitter

  16. #16
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will go throughwith thecode and will let you know

  17. #17
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cranial you have mentioned that:

    Quote Originally Posted by cranial-bore View Post
    The <label> tag should link to the appropriate form field. You look at the id attribute of the form field and put that same value in the for attribute of the label tag.
    that means that..

    How does php knows what information was given when you reference to a label field? Because the Label field is not the field receiving the text but the input field below it. I have observe that the for attribute value in the label field its the same value assigned to the name attribute in the input field. through the assignation of identical values to this attribute is how php knows what the user typed?

    I am thinking that the id attribute int he form field links to label name attribute, then the name attribute in the label field links to the name attribute value in the input field. is that correct guys?

    the form will have Both nickname and fullname fields just as Stomme poes. If I have been correct the only thing missing now is to send this values Global and make it available thorough the whole script with $_POST and $_GET. as I said before the comments form is going to be in the same page where the comments summited will be displayed. in other words the comment list will appear on top of the form.

    In the form values to be picked up to display are nickname, fullname, pros, and cons. But there is a field in the database call "created". the created field in the database will store the exact time when the user entered the information in the form. It will be stored automatically not manually by the user. I am tell you guys about the created database field so it is on mind at the moment of calling fields through the Global variable $_POST and $_GET to display the comments.

    thank you vaibhavcoder


    HTML Code:
    <form id="itemcomments" action="itemdetails2.php?id=12345678" method="post">
      <fieldset>
        <legend>Make a comment on this item</legend>
        <div>
          <label for="nickname">Nickname:</label>
          <input type="text" name="nickname" id="nickname" maxlength="85" />
        </div>
        <div>
          <label for="fullname">Full Name:</label>
          <input type="text" name="fullname" id="fullname" maxlength="85" />
        </div>
        <div>
          <label for="pros">Pros:</label>
          <textarea name="pros" id="pros" cols="35" rows="5"></textarea>
        </div>
        <div>
          <label for="cons">Cons:</label>
          <textarea name="cons" id="cons" cols="35" rows="5"></textarea>
        </div>  
        <input type="submit" name="submit" value="Add Comment">
        <input type="reset" value="Reset Fields">   
      </fieldset>
    </form>

  18. #18
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,287
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    How does php knows what information was given when you reference to a label field? Because the Label field is not the field receiving the text but the input field below it. I have observe that the for attribute value in the label field its the same value assigned to the name attribute in the input field. through the assignation of identical values to this attribute is how php knows what the user typed?
    The for and id partnership between labels and inputs is an HTML-only thing. It has nothing to do with PHP, and you will be writing those manually. If PHP is inserting the id's, then whatever variable contains that id, put that in the label's "for". No back-end language, PHP or anyone, will ever use that "for" attribute for matching what users type into the inputs. PHP never sees it, and doesn't need to.

    Whatever id you need on your inputs, just use those. THEN write the for="blah" in your labels, using whichever id you've chosen for your inputs.

    In the form values to be picked up to display are nickname, fullname, pros, and cons. But there is a field in the database call "created". the created field in the database will store the exact time when the user entered the information in the form. It will be stored automatically not manually by the user. I am tell you guys about the created database field so it is on mind at the moment of calling fields through the Global variable $_POST and $_GET to display the comments.
    I don't know if this is still considered Best Practice, but often I see people adding a hidden input in the form for additional values not inputted by the user.

    A better way is likely on the server-side. Your server obviously keeps track of time already, in Unix-epoch format likely. So, likely your script can simply look at what time it is on the server when the user hits "Submit" and add that as a value to your "created" field.

  19. #19
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your form is looking good now. The labels all relate to form fields.
    PHP doesn't care about <label> tags at all. They are purely a client side (i.e browser) thing. For most users they give a clickable element to focus the field, for users using assistive technology they make the form more accessible. Important, but totally irrelevant as far as PHP is concerned.

    So your form is correct. The script just needs to detect when it's been submitted -- the same script is executed with and without a form submission -- and put the $_POST data into the DB.
    You should be able to adapt the code I've already written for that.

    My first post (#4) showed how to store the created field value. The NOW() call in the query is a MySQL function that will record the current time when the query is executed.

  20. #20
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have this table caridadkb inside SELECT inside example.php which display all some of it's fields in this file called example.php, then when I click in one of this field in example.php it links me to itemdetails.php which will display each shoe information taken from the caridadkb table in detail. itemdetails.php has a form for comments as well. Inside this file I want to display the comments and put the form. The form action = itemdetails.php right? another thing is that I am passing the id value through the URL from example.php to itemdetails.php so it can be use to insert comments to each item that comes from example.php to itemdetails.php and that a user click again in the items the comments of that item from other user display according to the item id.

    Caridadkb table has an id and the comments table has an id

    caridadkb table:

    CREATE TABLE IF NOT EXISTS `caridadkb` (
    `id` int(1) NOT NULL AUTO_INCREMENT,
    `shoename_id` int(1) NOT NULL,
    `shoename` varchar(255) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
    `image` varchar(100) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
    `rating` int(4) NOT NULL,
    `position` int(1) NOT NULL,
    `price` decimal(9,2) DEFAULT NULL,
    `moreinfo` varchar(20) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
    `description` text CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=177 ;
    comments table:
    CREATE TABLE IF NOT EXISTS `comments` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `shoename_id` int(11) NOT NULL,
    `created` datetime NOT NULL,
    `author` varchar(255) NOT NULL,
    `pros` text NOT NULL,
    `cons` text NOT NULL,
    PRIMARY KEY (`id`),
    KEY `appetizers_id` (`appetizers_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
    so the final script would be something similar to this?

    PHP Code:
    <?php

    if(isset($_POST['Add Comment ' ])){ 
    $comment mysql_real_escape_string($comment);
    $author mysql_real_escape_string($author);
    $pros mysql_real_escape_string($pros);
    $cons mysql_real_escape_string($cons);
    $id = (int)['id']; 

    $query "INSERT INTO `comments` 
    (id created, author, pros, cons) 
    VALUES
    (
    $id, NOW(), '$author', '$pros', '$cons')";
    if(
    mysql_query($query)) {
        echo 
    "Thanks for your comment";
    }
    else {
        echo 
    "Unable to add your comment";
        
    //log mysql_error() here


    }

    echo
    '<form id="itemcomments" action="itemdetails2.php?id=12345678" method="post">
      <fieldset>
        <legend>Make a comment on this item</legend>
        <div>
          <label for="nickname">Nickname:</label>
          <input type="text" name="nickname" id="nickname" maxlength="85" />
        </div>
        <div>
          <label for="fullname">Full Name:</label>
          <input type="text" name="fullname" id="fullname" maxlength="85" />
        </div>
        <div>
          <label for="pros">Pros:</label>
          <textarea name="pros" id="pros" cols="35" rows="5"></textarea>
        </div>
        <div>
          <label for="cons">Cons:</label>
          <textarea name="cons" id="cons" cols="35" rows="5"></textarea>
        </div>  
        <input type="submit" name="submit" value="Add Comment">
        <input type="reset" value="Reset Fields">   
      </fieldset>
    </form>'
    ;
    ?>
    I know that should insert the comments according to the id in the URL valu coming from the example.php file. Now how can I display them right above the comments form?
    I know I will need some kind of loop but don't have any idea of how to script it.

  21. #21
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    stomme poes nice comment form, Now it's when I saw it and it is well structured thank you.
    The only thing I am missing is to put all this code together to work..


    I did this but it is not working...
    PHP Code:
    if(isset($_POST['Add Comment ' ])){ 

    $comment mysql_real_escape_string($_POST['author']);

    $author mysql_real_escape_string($_POST['author']);

    $pros mysql_real_escape_string($_POST['pros']);

    $cons mysql_real_escape_string ($_POST['cons']);

    $id = (int) $_GET['id']; 
    $query "INSERT INTO `comments` 

    (id created, author, pros, cons) 

    VALUES

    (
    $id, NOW(), '$author', '$pros', '$cons')";

    if(
    mysql_query($query)) {

        echo 
    "Thanks for your comment";

    }

    else {

        echo 
    "Unable to add your comment";

        
    //log mysql_error() here



    echo
    '<tr>';
    echo
    '<td>';
    echo
    '<table width="328" style="top:850px;" class="calamar">
    <td width="320" style=" line-height:3;"><strong>Comments:</strong></td>

    <tr></tr><td style="font-family:\'Times New Roman\', Times, serif; font-size:14px;">

    <form id="itemcomments" action="itemdetails2.php?id=12345678" method="post">

      <fieldset>

        <legend>Make a comment on this item</legend>

        <div>

          <label for="nickname">Nickname:</label>

          <input type="text" name="nickname" id="nickname" maxlength="85" />

        </div>

        <div>

          <label for="fullname">Full Name:</label>

          <input type="text" name="fullname" id="fullname" maxlength="85" />

        </div>

        <div>

          <label for="pros">Pros:</label>

          <textarea name="pros" id="pros" cols="35" rows="5"></textarea>

        </div>

        <div>

          <label for="cons">Cons:</label>

          <textarea name="cons" id="cons" cols="35" rows="5"></textarea>

        </div>  

        <input type="submit" name="submit" value="Add Comment">

        <input type="reset" value="Reset Fields">   

      </fieldset>

    </form>
    </table>'
    ;
    echo 
    '</td>';
    echo
    '</tr>'
    any inputs on this?

  22. #22
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are still identifying the form submission by the submit buttons VALUE instead of its NAME. Should be this:
    PHP Code:
    if(isset($_POST['submit'])) {


    This line is also wrong, I assume you can see why:
    PHP Code:
    $comment mysql_real_escape_string($_POST['author']); 

  23. #23
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you cranial I have modified with you help and other forum help to this:
    PHP Code:
    <?php

    if(isset($_POST['submit' ])){ 

    $nickname mysql_real_escape_string($_POST['nickname']);

    $fullname mysql_real_escape_string($_POST['fullname']);

    $pros mysql_real_escape_string($_POST['pros']);

    $cons mysql_real_escape_string ($_POST['cons']);

    $id2 = (int) $_GET['id']; 
    echo 
    $query "INSERT INTO comments VALUES (''',$shoename_id,' NOW(), '$nickname', '$fullname' ,'$pros', '$cons')";
    $result mysql_query($query) or die (mysql_error());

        echo 
    "Thanks for your comment";

    }

    ?>

    But still is not submitting the comments into the database...

    ps I have add the respective field into the table comments so it can match with the query variables in the script that we have been working on.

  24. #24
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was wondering it is needed to add something in the table form so it can insert the comments into the database beside the script that we have been working on.

    by the way it has come up to be a beautiful form guys... with the fieldset design and the labels

  25. #25
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be getting a MySQL error on your screen.

    You've buggered up the query, you have 3 single quotes in the VALUES () section and the quote and comma near $shoename_id are around the wrong way.

    You separate each value in your query with a comma. If it's a string type field (text/blob etc.) then you need to use single quotes around the PHP variable.
    PHP Code:
    $query "INSERT INTO comments VALUES (null, '$shoename_id', NOW(), '$nickname', '$fullname' ,'$pros', '$cons')"
    Passing an empty string to the auto increment isn't right anyway, you should pass in null (not a string), or use the full syntax I gave earlier and omit the auto increment entirely.


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
  •