tags:

views:

75

answers:

5

i want to select the most recent entry from a table and see if that entry is exactly the same as the one the user is trying to enter. How do I do a query to "select * from the most recent entry of 'posting'"?

    $query="Select * FROM 
        //confused here 
 (SELECT * FROM posting ORDER BY date_added DESC)
 WHERE user_id='{$_SESSION['user_id']}'
 AND title='$title'
 AND price='$price'
 AND city='$city'
 AND state='$state'
 AND detail='$detail'
 ";

 $data = mysqli_query($dbc, $query);
 $row = mysqli_fetch_array($data);
 if(mysqli_num_rows($data)>0)
 {
  echo "You already posted this ad. Most likely caused by refreshing too many times.";
  echo "<br>";
  $linkposting_id=$row['posting_id'];
  echo "See the <a href='ad.php?posting_id=$linkposting_id'>Ad</a>";
 }
 else
 {
        ...insert into the dbc
        }

//would this query work? or how do i use it to select the last ID in the table 'posting'?
    $query="SELECT LAST_INSERT_ID(posting)
     WHERE user_id='{$_SESSION['user_id']}'
     AND title='$title'
     AND price='$price'
     AND city='$city'
     AND state='$state'
     AND detail='$detail'
     ";
A: 

Limit your query to the last (most recent) entry with LIMIT 1

Brian Roach
A: 

You would usually have a unique identifier in your table and give that column an auto_increment. It's possible you already have such an identifier.

You can then fetch the latest record's ID using PHP's mysql_insert_id() or mySQL's LAST_INSERT_ID().

The latter would be preferable because according to the manual, it keeps the count per connection, so other processes or web pages can't screw up the count in between. It's not entirely sure from the PHP manual whether the PHP function does the same.

Pekka
I have posting_id which is auto_increment. So how exactly do I use last_insert_id() here?
ggfan
@ggfan mm, I see now that you are not creating the row in the same file. It could be that `last_insert_id()` will fail on that. Use @grossvogel's solution but add another column that identifies the user (e.g. a session ID) to prevent other ads being inserted at the same time screwing up your query.
Pekka
I believe last-insert-id is connection-specific. So it lets you retrieve that id right after the insert, in the same php script. You are asking to find the most recent record, potentially inserted by a different process, so it won't work here. You also want to match certain criteria, not selecting records inserted by other users, for instance. last_insert_id is a great feature, but not for this case.
grossvogel
-1 Pekka, I usually agree with what you have to bring to the table, but this advice is not appropriate IMO. First off, `mysql_insert_id()` will _indeed_ keep count per connection, but a new request will not be the same connection! ;-) Second, I wouldn't rely on auto_increment fields for keeping track of insert time. They are only ment to uniquely identify your record. You should use a timestamp or date/time column for keeping track of these things.
fireeyedboy
@fireeyedboy and @grossvogel your input is very good, but I beat you both in my comment above, realizing the same things :D fireeyedboy with your second point I have to disagree - fetching the record with the latest creation date is not going to be reliable, because another instance/user could have created a record in the meantime, rendering the query useless. You need some sort of unique identifier to find out whether *this user* has recently added a record. I would suggest using the user's session ID or (at least) IP for that.
Pekka
@Pekka: You're right about catching it in your comment. Good point. I was still typing, but didn't wanna refrain from posting it anyway. But I wholeheartedly agree with your statement about a user id. I assumed OP had this covered already though. It's hard to tell from the code whether OP actually has. And I assume you understand yourself that auto_increment is simply not gonna do it either in this case. You need a combo of user id and datetime/timestamp. I wouldn't rely on a combo of user id and auto_increment.
fireeyedboy
@fireeyedboy yup. The only scenario where auto_increment would do is when the row creation takes place during the same connection, which I later realized is not the case here.
Pekka
@Pekka: Yeah, I kind of assumed you were probably mistaken by accident, cause I couldn't match this post with your otherwise helpful and correct posts.
fireeyedboy
+2  A: 

The orderby piece needs to come at the end of your query. So the query you're asking for is something like this:

select * from posting where .... order by date_added desc limit 1;

Single-use form tokens can help prevent duplicate submissions, too.

grossvogel
This will make problems if two or more users create ads at the same time. It would need another column uniquely identifying the posting user to work.
Pekka
much thanks!!!!
ggfan
+1  A: 

I suggest creating a unique key on your table instead of doing what you described. This way you avoid duplicated content independent of how many users are using your application concurrently. Your solution would fail if A inserts something, B inserts something else, then A submits again.

Example of adding a unique key to an existing table:

ALTER TABLE posting add UNIQUE KEY uk_posting (title, price, city)

Replace title, price, city with the fields combination that must be unique. Then all you have to do is to handle the error upon insert.

jweyrich
Good idea, though this might be a bit too restrictive, depending on the business requirements.
grossvogel
A: 

Based on your comments, either of the following queries will retrieve the single latest record:

SELECT * FROM posting ORDER BY date_added DESC LIMIT 1

SELECT * FROM posting ORDER BY posting_id DESC LIMIT 1
Dolph