单选按钮列表

时间:2012-02-22 13:45:02

标签: php

我想知道是否有人可以帮助我。

我使用下面的代码构建一个带有单选按钮的列表,作为选择每一行数据的方法。

<?php

mysql_connect("hostname", "username", "password")or
die(mysql_error());
mysql_select_db("database");



$result = mysql_query("SELECT userdetails.userid, finds.userid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname, finds.dateoftrip, finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1");
if (mysql_num_rows($result) == 0)
// table is empty
  echo 'There are currently no finds recorded for this location.';
echo"<table>\n";
  while (list($userid, $dateoftrip) =
    mysql_fetch_row($result))
  {

    echo"<tr>\n"
    .
     "<td><input type='radio' name='radio' dateoftrip, value='{$userid}' /></td>\n"
    ."<td><small>{$dateoftrip}</small><td>\n"
    ."</tr>\n";
  }
  echo"<tr><td colspan=\"3\">";
  echo"<br>";
  echo"</td></tr>";
  echo'</table>';

?>

我可以设法构建列表,但单选按钮旁边的值是&#39; userid&#39;而我正打算把日期转移到#39;针对每个单选按钮。

我已经在这方面工作了一段时间,我无法看到我的错误。

我只是想知道是否有人可以看看这个请让我知道我哪里出错了。

非常感谢

3 个答案:

答案 0 :(得分:2)

你的问题在这一行:

while (list($userid, $dateoftrip) =

鉴于查询是:

SELECT userdetails.userid, finds.userid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname, finds.dateoftrip, finds.findname, .....

list()按SELECT查询返回的顺序分配变量,前两个结果列实际上是userid。

我建议使用:

while ($row = mysql_fetch_assoc($result))   {
[...]
"<td><small>".$row["dateoftrip"]."</small></td>\n"  // you also have a typo here 

或将您的查询更改为只按该顺序返回userid和dateoftrip

答案 1 :(得分:1)

mysql_fetch_row()从结果资源返回一个数字索引数组,其中的字段与您选择的字段顺序相同。由于您的SELECT列表的前两个字段为userdetails.userid, finds.userid,因此这两个字段会返回到您的list()电话。

相反,使用mysql_fetch_assoc()更容易。您可以使用mysql_fetch_row()并找出所需的两列的数字索引,但这很困难。在最好的情况下,您首先只需在SELECT中包含实际需要的两列,这将使您的代码正常工作。

while ($row = mysql_fetch_assoc($result)) {
  $userid = $result['userid'];
  $dateoftrip = $result['dateoftrip'];
  // etc...
}

请注意,由于您从不同的表中获取了两个名为userid的列,因此您应该为它们创建别名,以便在获取后区分它们:

// Aliases for userdetails.userid, finds.userid:
$result = mysql_query("SELECT userdetails.userid AS userid, finds.userid AS findsid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname, finds.dateoftrip, finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1");

答案 2 :(得分:1)

您正在选择比您使用的数据更多的数据。尝试减少SELECT语句,以便选择的第一个字段是用户ID,第二个字段是行程日期。那么也许你可以希望你的list陈述能够按你的意愿运作。

相关问题