gpt4 book ai didi

php - if else语句与mysql表? - PHP

转载 作者:可可西里 更新时间:2023-11-01 08:29:17 25 4
gpt4 key购买 nike

我需要做一个 if else 语句来阻止用户出价最高的项目。我认为它会像这样。

if ($accountid = the accountid in the bidhistory table)
{
echo "You are the highest bidder!";

}
else
{
$sql="INSERT INTO bidhistory (accountid, biditemid)
VALUES ($accountid, $itemid)";

mysql_query("
UPDATE bidhistory
SET bidprice = bidprice + 1
WHERE biditemid = " .
@mysql_escape_string($itemid));

$result=mysql_query($sql) or die("Error in adding bid for item: ".mysql_error());
}
?>

我不确定如何从 bidhistory 表中引用 accountid。另外,如果有更好的方法可以做到这一点,请指出莱特的方向。谢谢!

最佳答案

备注:

  • 您可以首先获取特定项目出价最高的行。然后检查最高出价者(accountid)是否与当前用户相同。
  • 您可以查看我对 /* ... */ 中引用的某些行所做的注释

您要检查的查询:

$result = mysql_query("SELECT accountid FROM bidhistory WHERE biditem = '$itemid' ORDER BY bidhistoryid DESC LIMIT 1"); /* GET THE LAST ROW FOR WHO BIDS LAST; AND REPLACE NECESSARY COLUMN NAME (unique/primary) - bidhistoryid */
while($row = mysql_fetch_array($result)){
$checkaccountid = $row['accountid']; /* STORE THE USER THAT BIDS LAST FOR THIS ITEM */
}

if($checkaccountid == $accountid){ /* THEN COMPARE IT WITH THE CURRENT USER */
/* CODE YOU WANT TO DO IF HE/SHE IS THE LAST BIDDER ALREADY */
}
else {
/* IF NOT, HE/SHE CAN STILL BID */
}

但我建议您使用 mysqli_* 而不是 deprecated mysql_* API。

$con = new mysqli("YourHost", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

if($stmt = $con->prepare("SELECT accountid FROM bidhistory WHERE biditem = ? ORDER BY bidhistoryid DESC LIMIT 1")){
$stmt->bind_param("i",$itemid); /* BIND THIS VARIABLE TO YOUR QUERY ABOVE */
$stmt->execute(); /* EXECUTE THE QUERY */
$stmt->bind_result($checkaccountid); /* STORE THE RESULT TO THIS VARIABLE */
$stmt->fetch(); /* FETCH THE RESULT */

if($checkaccountid == $accountid){
/* CODE YOU WANT TO DO IF HE/SHE IS THE LAST BIDDER ALREADY */
}
else {
/* IF NOT, HE/SHE CAN STILL BID */
}
$stmt->close();

} /* END OF PREPARED STATEMENT */

关于php - if else语句与mysql表? - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396938/

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