If else statement with mysql table? - PHP

I need to make an if else statement that will discourage the user from bidding on an item if it is the highest price. I think it will be a bit like this.

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());
}
?>

      

I'm not sure how to reference the account from the bidhistory table. Also, if there is a better way to do this, please point me in directions. Thank!

+3


source to share


1 answer


Note:

  • You can get the highest bid row first. Then check if the highest bid ( accountid

    ) matches the current user.
  • You can check the entries I had for some of the lines mentioned in /* ... */

Your request for verification:



$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 */
}

      

But I recommend using the deprecated API mysqli_*

instead . mysql_*

$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 */

      

+2


source







All Articles