SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,899
    Mentioned
    212 Post(s)
    Tagged
    12 Thread(s)

    How to access the last record in a database table using PHP

    Hi,

    I'm making a very simple form in PHP. It has three fields 'name', 'email' and 'comment'. Upon form submission these fields get written to a database table.

    So as to prevent double data submission I'm trying to do the following: when a user clicks send, I look up the previous record in the database and compare the comment field of that record with the comment field of the data that is being submitted. If they are the same, then I would discard the submission.

    Two questions:
    a) How would I go about getting the comment field from the previously submitted record into a variable within my PHP script?

    So far I have:
    Code PHP:
    $query = "SELECT * FROM guestbook ORDER BY id DESC limit 0,1";
    $last = mysql_query($query);
    which (I think) gets me the previous record. But how can I access the field 'comment' from within this record. ($last.comment would work in rails).

    b) Is this a good / sensible way to stop double data submission in a simple form.

    Thanks in advance

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You could store a hash (md5, sha1 et al) of the last comment made by that user in their $_SESSION, this would prevent them submitting the same comment twice.

    I'd also use something unique to the subject matter that they are commenting on too, just in case they want to add "I agree" to more than one commentable item.

    Something like...

    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['last_comment_hash'] = md5($post_id $_POST['comment']);
    This would prevent the round trip to the database. Additionally, take a look at Post, Redirect, Get pattern.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    If you are worried about users clicking the submit button twice in rapid succession, I would just disable the submit button with javascript once it's clicked.

    Beware, do not do this in the onsubmit of the form. If you do IE won't submit anymore, at all. Instead put it in the onclick of the submit button.

    If you're using jQuery it would be:
    Code javascript:
    $('input[type="submit"]').click( function() { $(this).attr('disabled','disabled'); } );

    If you are worried people might reload the page and resubmit the data, look at post-refresh-get like Anthony just said
    Rmon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    UNIQUE index would be helpful.

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,899
    Mentioned
    212 Post(s)
    Tagged
    12 Thread(s)
    Thank you all for the answers. They were very helpful.
    I didn't know about the Post, Redirect, Get pattern, so reading about that was very interesting.
    On reflection, this is a small, temporary site for family and friends, so it is perfectly adequate to use JavaScript to disable the send button.
    I have read a bit about that now and have a couple of questions, but I'll head over to the JavaScript forum and post them there.


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
  •