在单选按钮上检索mySQL数据单击

时间:2012-02-29 15:10:16

标签: php mysql ajax

通过本网站用户的一些非常感谢的帮助,我已经能够整理一个脚本,单击单选按钮将填充一个包含用户详细信息的表。

我认为我能够进一步调整它,但很可能是因为我缺乏经验,不幸的是我遇到了另一个问题,因此我添加了一个新帖子。

从mySQL数据库中提取数据我正在使用下面的代码创建一个带有相关单选按钮的日期列表。

<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser2.php?="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php 
mysql_connect("hostname", "username", "password")or 
die(mysql_error()); 
mysql_select_db("database"); 


$result = mysql_query("SELECT userdetails.userid, finds.dateoftrip, detectinglocations.locationname, finds.userid, finds.locationid, detectinglocations.locationid,   finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1 GROUP By dateoftrip ORDER BY dateoftrip DESC"); 

if (mysql_num_rows($result) == 0)  
// table is empty  
  echo 'There are currently no finds recorded for this location.';  
  else 
 {  
  echo"<table>\n"; 
  while (list($userid, $dateoftrip) = 
    mysql_fetch_row($result)) 
  { 

    echo"<tr>\n" 
    . 
     "<td><input type='radio' name='show' dateoftrip value='{$userid}' onClick='showUser(this.value)'/></td>\n" 
    ."<td><small>{$dateoftrip}</small><td>\n" 
    ."</tr>\n"; 
  } 
  echo'</table>'; 
} 

?> 
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>

然后使用以下代码,我想填充一个表,其中包含单击的单选按钮的相关“findname”详细信息。

<?php
$q=$_GET["q"];

$con = mysql_connect('hostname', 'username', 'password');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('database', $con);

$sql="SELECT * FROM finds WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Find Name</th>
</tr>";

while($row = mysql_fetch_array($sql))
  {
  echo "<tr>";
  echo "<td>" . $row['findname'] . "</td>";

  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

我可以让脚本的第一部分工作,即创建日期列表和单选按钮,但是当我选择单选按钮时,表格会显示正确的列标题,但是我收到以下错误:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/2/d333603417/htdocs/development/getuser2.php on line 21第21行是这一行:while($row = mysql_fetch_array($sql))

正如我之前所说,回复我的第一篇文章的其他用户都很棒,但我只是想知道是否有人可以看看这个,并让我知道我哪里出错了。

更新代码

表格

<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser2.php?="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php 
mysql_connect("hostname", "username", "password")or 
die(mysql_error()); 
mysql_select_db("database"); 


$result = mysql_query("SELECT userdetails.userid, finds.dateoftrip, detectinglocations.locationname, finds.findid, finds.userid, finds.locationid, detectinglocations.locationid, finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.locationid=detectinglocations.locationid AND finds.userid = 1 GROUP By dateoftrip ORDER BY dateoftrip DESC"); 

if (mysql_num_rows($result) == 0)  
// table is empty  
  echo 'There are currently no finds recorded for this location.';  
  else 
 {  
  echo"<table>\n"; 
  while (list($findid, $dateoftrip) = 
    mysql_fetch_row($result)) 
  { 

    echo"<tr>\n" 
    . 
     "<td><input type='radio' name='show' dateoftrip value='{$findid}' onClick='showUser(this.value)'/></td>\n" 
    ."<td><small>{$dateoftrip}</small><td>\n" 
    ."</tr>\n"; 
  } 
  echo'</table>'; 
} 

?> 
<br />
<div id="txtHint"></div>
</body>
</html>

PHP

    <?php 
//$q=$_GET["q"]; 

$con = mysql_connect('hostname', 'username', 'password'); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db('database', $con); 

$sql="SELECT * FROM finds"; 

$result = mysql_query($sql); 

// This is helpful for debugging
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

echo "<table border='1'> 
<tr> 
<th>Find Name</th> 
</tr>"; 

while($row = mysql_fetch_array($result)) 
{ 
echo "<tr>"; 
echo "<td>" . $row['findname'] . "</td>"; 

echo "</tr>"; 
} 
echo "</table>"; 

mysql_close($con); 
?> 

2 个答案:

答案 0 :(得分:1)

while ($row = mysql_fetch_array($result))

不是

while ($row = mysql_fetch_array($sql))

mysql_fetch_array接受一个mysql结果对象(你从mysql_query函数调用中获得),而不是一个字符串

答案 1 :(得分:1)

$row = mysql_fetch_array($sql)

$sql是一个字符串,您应该使用$result代替mysql_result对象。