简体   繁体   English

难以从两个表获取数据

[英]Difficulty in getting data from two tables

As a self-taught (poorly) PHP coder, I have exhausted all the trial-and-error possibilities so I humbly ask your help for a small problem I have been having while coding a community online "bidding" game. 作为一个自学成拙的(拙劣的)PHP编码器,我穷尽了所有的反复试验的可能性,因此我谦虚地向您寻求帮助,以解决编写社区在线“竞标”游戏时遇到的一个小问题。

I have two tables in my DB - one, called DCO_sponsors, listing a list of IDs, an associated amount of (virtual!) money, an availability counter and (if they are already associated with a team), the team's id. 我的数据库中有两个表-一个叫做DCO_sponsors的表,列出了ID列表,相关的(虚拟!)钱,可用性计数器以及(如果它们已经与团队关联)团队的ID。

Structure: id, amount, available, team_id 结构:id,金额,可用,team_id

The second table, DCO_bids, lists the team's "bids" to clinch the above mentioned sponsors. 第二个表DCO_bids列出了团队的“出价”,以争取上述赞助商。 It includes a bid id, the team id, a "part_b" which in this case is the sponsor id (the same table is used by other processes), a concept field where the kind of bid is described (eg "sponsorship") and finally two fields for the bid amount and the current status (A for active, W for waiting/pending, etc). 它包括投标ID,团队ID,“ part_b”(在这种情况下为发起人ID)(其他过程使用同一表),描述投标类型的概念字段(例如“赞助”)和最后是两个字段,分别是出价金额和当前状态(A表示有效,W表示等待/等待中,等等)。

Structure: id, team_id, part_b, concept, bid, status 结构:id,team_id,part_b,概念,出价,状态

What I want to achieve is the site to show me (the admin) a list of all the sponsors in the DCO_sponsors table, ordered by the amount, and next to it the team_id of the team which bid the highest for it (teams only get four "sponsors" and get the highest grossing first, so it's important I see them in order of amount). 我想要实现的站点是向我(管理员)显示DCO_sponsors表中所有赞助商的列表,并按金额排序,然后在该列表旁边为该出价最高的团队的team_id(团队只能得到四个“赞助商”,并获得最高的总收入,因此,按金额顺序查看它们很重要)。

The code I produced, and that I hoped it would work, is as follows: 我产生的代码,希望它能起作用,如下所示:

            echo "<table>";
        echo "<tr><th width=30>Id</th><th width=100>Amount</th><th width=220>Team</th><th width=100>Offer</th><th></th><th></th><th></th></tr>";

        $data = mysql_query("SELECT id, amount FROM DCO_sponsors WHERE available='1' ORDER BY amount DESC", $CONNECTW);
        while($row=mysql_fetch_row($data))
        {
            $sp_id = "$row[0]";
            $sp_amount = number_format($row[1]);

            $teamdata = mysql_query("SELECT team_id, bid FROM DCO_bids WHERE concept='sponsorship' AND part_b='$sp_id' ORDER BY bid DESC", $CONNECTW);
            while($row=mysql_fetch_row($teamdata))
            {
                $team_id = "$row[0]";
                $team_bid = number_format($row[1]);                     
            }               

            // I call the name of the team from another table

            $teamlabel = mysql_query("SELECT completename FROM DCO_teams WHERE id='$team_id'", $CONNECTW);
            while($row=mysql_fetch_row($teamdata))
            {
                $teamname = "$row[0]";                      
            }

            echo "<tr><td>$sp_id</td><td>&pound;$sp_amount</td><td>$teamname</td><td>$team_bid</td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A1&item_id=$item_id'><div id='IC_Tick' title='Sign Round 1 deal with $teamname'></div></a></td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A2&item_id=$item_id'><div id='IC_Tick' title='Sign Round 2 deal with $teamname'></div></a></td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A3&item_id=$item_id'><div id='IC_Tick' title='Sign Round 3 deal with $teamname'></div></a></td></tr>";

        }

        echo "</table>";

The links in the last cells of the row are tick-boxes linked to actions that modify other tables. 该行最后一个单元格中的链接是复选框,链接到修改其他表的操作。

At the moment, the result I get is the list of the sponsors ordered by the amount (as it should be), but the team name appearing alongside it is the same for all rows, it's NOT the one of the highest bidder and the bid amount is NOT the one of the highest bidder but simply the first one to put one in. 目前,我得到的结果是按金额排序的赞助商列表(应该如此),但旁边显示的团队名称对于所有行都是相同的,不是出价最高的人和出价最高的人之一金额不是出价最高的人之一,而仅仅是第一个出价最高的人。

I spent the best part of the last three nights trying to work out where I am getting this wrong... I hope you can help! 在过去的三个晚上中,我度过了最美好的时光,试图找出错误原因所在...希望您能为您提供帮助!

<table>
  <tr>
    <th width=30>Id</th>
    <th width=100>Amount</th>
    <th width=220>Team</th>
    <th width=100>Offer</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
<? $data = mysql_query("SELECT id, amount FROM DCO_sponsors WHERE available='1' ORDER BY amount DESC", $CONNECTW); ?>

<? while($row=mysql_fetch_row($data)):
    $sp_id = $row['id'];
    $sp_amount = number_format($row['amount']);

    $teamdata = mysql_query("SELECT team_id, bid FROM DCO_bids WHERE concept='sponsorship' AND part_b='$sp_id' ORDER BY bid DESC", $CONNECTW);
    while($row=mysql_fetch_row($teamdata))
    {
        $team_id = $row['id'];
        $team_bid = number_format($row['bid']);                     
    }               

        $teamlabel = mysql_query("SELECT completename FROM DCO_teams WHERE id='$team_id'", $CONNECTW);
        while($row=mysql_fetch_row($teamlabel))
        {
            $teamname = $row['completename'];                      
        }
?>
    <tr>
      <td><?=$sp_id;?></td>
      <td>&pound;<?=$sp_amount;?></td>
      <td><?=$teamname;?></td>
      <td><?=$team_bid;?></td>
      <td><a href='<?=$RKP;?>/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A1&item_id=<?=$item_id;?>'>
        <div id='IC_Tick' title='Sign Round 1 deal with <?=$teamname;?>'></div></a></td>
      <td><a href='<?=$RKP;?>/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A2&item_id=<?=$item_id;?>'>
        <div id='IC_Tick' title='Sign Round 2 deal with <?=$teamname;?>'></div></a></td>
      <td><a href='<?=$RKP;?>/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A3&item_id=<?=$item_id;?>'>
        <div id='IC_Tick' title='Sign Round 3 deal with <?=$teamname;?>'></div></a></td>
    </tr>

<? endwhile; ?>
</table>

Your issue came into play with your last while call. 您的问题与您上一次通话的时间有关。 You were attempting to mysql_fetch_row($teamdata) again instead of $teamlabel. 您正在尝试再次使用mysql_fetch_row($ teamdata)而不是$ teamlabel。

Also, as a rule, you should just write out any html that doesn't need to be generated by php. 同样,通常,您应该只写出不需要php生成的任何html。 So I broke that stuff out for you. 因此,我为您介绍了这些内容。

Fixed the problem - here is the code that works: 解决了问题-这是有效的代码:

echo "<table>";
    echo "<tr><th width=30>Id</th><th width=100>Amount</th><th width=220>Team</th><th width=100>Offer</th><th></th><th></th><th></th></tr>";

    $query = mysql_query("SELECT id, amount FROM DCO_sponsors WHERE available = '1' ORDER BY amount DESC");
    while($row = mysql_fetch_assoc($query)){
        $sp_id = $row['id'];
        $sp_amount = $row['amount'];
        $sp_amount = number_format($sp_amount);
        $tQuery = mysql_query("SELECT team_id, bid, id FROM DCO_bids WHERE concept='sponsorship' AND part_b='$sp_id' AND status='W' ORDER BY bid DESC");
        $trow = ' ';
        $trow = mysql_fetch_assoc($tQuery);
        $team_id = $trow['team_id'];
        $team_bid = $trow['bid'];
        $team_bid = number_format($team_bid);
        $item_id = $trow['id'];
        $nquery = mysql_query("SELECT completename FROM DCO_teams WHERE id='$team_id'");
        $nrow = mysql_fetch_assoc($nquery);
        $teamname = $nrow['completename'];


        echo "<tr><td>$sp_id</td><td>&pound;$sp_amount</td><td>$teamname</td><td>&pound;$team_bid</td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A1&item_id=$item_id'><div id='IC_Tick' title='Sign Round 1 deal with $teamname'></div></a></td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A2&item_id=$item_id'><div id='IC_Tick' title='Sign Round 2 deal with $teamname'></div></a></td><td><a href='$RKP/kernel/lib/php_lib/action/AC_Bids_Update.php?op=A3&item_id=$item_id'><div id='IC_Tick' title='Sign Round 3 deal with $teamname'></div></a></td></tr>";
    }

     echo "</table>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM