[英]Retrieve mySQL Data On Radio Button Click
通過本網站用戶的一些非常贊賞的幫助,我已經能夠整理一個腳本,單擊單選按鈕將填充一個包含用戶詳細信息的表。
我認為我能夠進一步調整它,但很可能是因為我缺乏經驗,不幸的是我遇到了另一個問題,因此為什么我添加了一個新帖子。
從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
行Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/2/d333603417/htdocs/development/getuser2.php on line 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);
?>
while ($row = mysql_fetch_array($result))
不
while ($row = mysql_fetch_array($sql))
mysql_fetch_array
接受一個mysql結果對象(你從mysql_query
函數調用中得到),而不是一個字符串
在$row = mysql_fetch_array($sql)
$sql
是一個字符串,你應該使用$result
代替,這是一個mysql_result
對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.