-1

我对 php 和 myadmin 非常缺乏经验,并且一直在尝试利用一些教程来使用 ajax 来查询数据库。我首先想遍历数据库,给我一个可供选择的选项的下拉列表,即:Food Petrol Shopping Entertainment 然后我希望用户能够选择其中一个下拉选项,然后这将查询数据库并生成一个包含该选择数据的表格,即如果他们选择汽油,它将生成一个表格 收款人金额日期 Tesco 23.00 27/10/13 Sainsbury 20.00 20/10/13 等

这是我的ajax代码

    <html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("output").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("output").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","catagory.php?catagory="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>


<?php

$server = 'localhost';
$user='root';
$pass='';
$db = 'finance_checker';

$mysqli = mysqli_connect($server, $user, $pass, $db);

$query = $mysqli->query("SELECT distinct `catagory` FROM `transactions`");

while($array[]= $query->fetch_object());

array_pop($array);

?>


        <h3>Transactions</h3>
        <select name="the_name" onchange="showUser(this.value)">
            <?php foreach ($array as $option): ?>
            <option value="<?php echo $option->Transaction; ?>"><?php echo $option -> catagory;?></option>
            <?php endforeach; ?>
        </select>
        <div id="ouput"<b>Transactions:</b></div>
        <?php
        $query-> close();
        ?>


</body>
</html> 

这是我查询数据库的代码:

    <?php
$q = $_GET['catagory'];

$con = mysqli_connect('localhost','root','','finance_checker');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db($con,"finance_checker");
$sql="SELECT `ThirdParty`, `Credit`,`Date` FROM `transactions` WHERE `catagory` = '".$q."'";

$result = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Payee</th>
<th>Amount</th>
<th>Date</th>

</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ThirdParty'] . "</td>";
  echo "<td>" . $row['Credit'] . "</td>";
  echo "<td>" . $row['Date'] . "</td>";

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

mysqli_close($con);
?>

初始代码正在生成正确的下拉选项,但是当我选择其中一个选项时,我没有得到任何复制。我以为我已经理解发生了什么,但很明显我错过了一些东西,谁能提供进一步的指导?

4

2 回答 2

0

当您在选择框中加载数据时,请检查选择查询。试试下面一个

$query = $mysqli->query("SELECT distinct `catagory`,`Transaction` FROM `transactions`");
于 2013-10-27T13:48:43.627 回答
0

既然你只...

SELECT distinct `catagory` FROM `transactions`

...$option->Transaction总是未定义的。

于 2013-10-27T13:38:26.717 回答