Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带mysql表的If-else语句PHP_Php_Mysql_If Statement - Fatal编程技术网

带mysql表的If-else语句PHP

带mysql表的If-else语句PHP,php,mysql,if-statement,Php,Mysql,If Statement,我需要做一个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)"; mysq

我需要做一个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\u*
而不是
mysql\u*
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 */

bidhistory
表中,它是否有主键?像
bidhistory\u id
?所以它将是唯一的。@LoganWayne是的,它是唯一的。我一直收到一个错误,说$checkaccountid未定义。@LuluSparks-您是否用相应的列名(
bidhistoryid
)更改了查询?或者尝试在服务器端运行此查询
从bidhistory中选择accountid,其中biditem=1 ORDER BY bidhistoryid DESC
或使用任何有效的
biditem
编号。你用了什么?我的第一套代码,还是第二套?我用了第一套。你说我更改了查询是什么意思?@LuluSparks-我指的是
bidhistoryid
。您的
bidhistory
表的唯一/主键id是什么?@LuluSparks-尝试在服务器端(在您的PhpMyAdmin页面中)运行此查询
从bidhistory中选择accountid,其中biditem=1 ORDER BY bidhistoryid DESC
。让我们看看是否有结果。