【问题标题】:Retrieve mySQL Data On Radio Button Click在单选按钮单击时检索 mySQL 数据
【发布时间】:2012-02-29 15:10:15
【问题描述】:

通过本网站用户的一些非常感谢的帮助,我已经能够编写一个脚本,单击单选按钮后,该脚本将在表格中填充用户详细信息。

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

从 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>

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

<?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); 
?> 

【问题讨论】:

  • 如何在您的第一个代码脚本 mysql_query 中找到 'WHERE finds.userid=userdetails.userid AND ... AND finds.userid = 1'?
  • @sransara 谢谢你,我现在已经把finds.userid=userdetails.userid 从我的查询中去掉了。问候

标签: php mysql ajax


【解决方案1】:
while ($row = mysql_fetch_array($result))

不是

while ($row = mysql_fetch_array($sql))

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

【讨论】:

  • 您好,能否详细说明一下?
【解决方案2】:

$row = mysql_fetch_array($sql)

$sql 是一个字符串,你应该使用$result,它是一个mysql_result 对象。

【讨论】:

  • 嗨,非常感谢您帮我看这个。我已经进行了您建议的更改,但不幸的是我仍然收到相同的错误。亲切的问候
  • @IRHM 你检查过你的“查询结果”是否给出了正确的结果——也许试试here中的第一个例子
  • 嗨@sransara,如果我理解正确,那么结果如下:Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 1=1' at line 1亲切的问候
  • 嗨@IRHM 实际上我早期的意思是在做类似this one
  • 抱歉,我的速度太慢了。我运行了调试。这一行的“id”$sql="SELECT * FROM finds WHERE id = '".$q."'";是问题所在。我现在已经更改了这个以及与“获取数组”相关的那些.该表现在显示字段数据。但是它没有显示选择正确单选按钮的相关记录。这是我页面的链接:mapmyfinds.co.uk/development/dateoftrip.php。我对此有点陌生,所以我用新代码修改了我原来的帖子。我只是想知道你是否可以看看它,让我知道我哪里出错了。真诚的感谢。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多