gpt4 book ai didi

php - 如何选择 mysql 中的最新条目?

转载 作者:可可西里 更新时间:2023-11-01 06:56:01 26 4
gpt4 key购买 nike

我想从表中选择最近的条目,看看该条目是否与用户尝试输入的条目完全相同。我如何查询“从最近的‘发布’条目中选择 *”?

    $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'
";

最佳答案

orderby 部分需要出现在查询的末尾。所以你要求的查询是这样的:

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

一次性表单标记也有助于防止重复提交。

关于php - 如何选择 mysql 中的最新条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2659720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com