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 inside while loop() check each item if items auctioned users or not.
  • it easier if used accountid username (auction by) in biditems table can connect table account table faster , easier on part of indexing. still, can use inner join connect biditems table account table using username 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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -