[英]How do I select the most recent entry in mysql?
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'
";
The orderby piece needs to come at the end of your query. orderby片段需要在查询结束时出现。 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. 一次性表单令牌也可以帮助防止重复提交。
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
You would usually have a unique identifier in your table and give that column an auto_increment
. 您通常在表中有一个唯一标识符,并为该列提供
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() . 然后,您可以使用PHP的mysql_insert_id()或mySQL的LAST_INSERT_ID()获取最新记录的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.
从PHP手册中可以确定PHP函数是否也是如此。
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.
如果A插入一些东西,B插入其他东西,然后A再次提交,你的解决方案将失败。
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. 将
title, price, city
替换title, price, city
必须唯一的字段组合。 Then all you have to do is to handle the error upon insert. 然后你要做的就是在插入时处理错误。
使用LIMIT 1
将查询限制为最后一个(最近的)条目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.