mysql - PHP Select * from two different tables? -
i'm making simple auction website , want keep user bidding on item when highest bidder, want keep item being displayed.
i have table called bidhistory stores accountid of bidder. have below i'm using prevent user bidding on own item. correct syntax selecting bidhistory table well? hope i'm being clear enough.
$sql="select * biditems not accountid = $accountid";
more of code:
$accountid=$_session['accountid']; $sql=" select * biditems not accountid = $accountid"; $result=mysql_query($sql); echo "items auction"; $expireday=new datetime('2015-07-19 20:12:50'); $theday=new datetime('now'); $timetoend=$expireday->diff($theday); echo $timetoend->format('%r%a days'); while($row=mysql_fetch_array($result)) { $itemid=$row['itemid']; $item=$row['biditem']; $auctionby=$row['username']; $description=$row['biddesc']; echo "<ul>"; echo "<li>$item auctioned off $auctionby. <br> description: $description <br> <a href='acceptbid.php?itemid=$itemid&item=$item'>place bid</a></li>"; echo "</ul>"; } ?>
and here tables.
$sql = "create table account ( accountid int(100) unsigned auto_increment primary key, username varchar(30) not null, password varchar(30) not null )"; $sql = "create table biditems ( itemid int(100) unsigned auto_increment primary key, accountid int(100), biditem varchar(30) not null, biddesc tinytext, dateadded datetime )"; $sql = "create table bidhistory ( bidhistoryid int(100) unsigned auto_increment primary key, accountid int(100), biditemid int, bidprice int not null, biddate datetime )";
note:
- you want list items bidding, don't want user bid on his/her own item? can fetch bid items, basic
if() else()
statement insidewhile loop()
check each item if items auctioned users or not. - it easier if used
accountid
username
(auction by) inbiditems
table can connect tableaccount
table faster , easier on part of indexing. still, can useinner join
connectbiditems
tableaccount
table usingusername
column (hoping usernames unique).
you can work this:
$sql=" select a.itemid, a.biditem, a.biddesc, b.accountid, b.username biditems inner join account b on a.username = b.username"; /* query above fetch bid items */ $result=mysql_query($sql); /* execute query */ echo "items auction"; $expireday=new datetime('2015-07-19 20:12:50'); $theday=new datetime('now'); $timetoend=$expireday->diff($theday); echo $timetoend->format('%r%a days'); while($row=mysql_fetch_array($result)) { $itemid=$row['itemid']; $item=$row['biditem']; $auctionby=$row['username']; $description=$row['biddesc']; $auctionbyid = $row['accountid']; /* accountid of auction-er */ $result2 = mysql_query($con,"select accountid bidhistory biditem = '$itemid' order bidhistoryid desc limit 1"); while($row2 = mysql_fetch_array($result2)){ $checkauctionid = $row2['accountid']; } echo ' <ul> <li>'.$item.' auctioned off '.$auctionby.'<br> description: '.$desription.'<br>'; if($accountid != $auctionbyid && $accountid != $checkauctionid){ /* if item not auctioned user or he/she not highest bidder */ echo '<a href="acceptbid.php?itemid='.$itemid.'&item='.$item.'">place bid</a></li>'; } /* end of if $accountid not same $actionbyid */ else { /* else, means item his/hers, he/she cannot bid on his/her own item or highest bidder item */ echo 'cannot place bid'; } /* end of else */ echo '</ul>'; } /* end of while loop */
- make sure in
acceptbid.php
file query still checks if item viewing not auctioned user. - and again, refrain using
mysql_*
api deprecated already.
Comments
Post a Comment