使用PHP使用下拉列表显示数据库中的数据

时间:2014-04-21 10:25:50

标签: php mysql

我正在尝试使用PHP根据http://www.w3schools.com/php/php_ajax_database.asp的代码显示数据库中的数据 我没有使用ID列,而是使用了讲座表中的TITLE列,但它不起作用。什么都没有显示(没有警告,没有任何东西) 我怎样才能做到这一点?这是我的代码

<script>
    function showReport2(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","LectureReport.php?q="+str,true);
        xmlhttp.send();
    }
</script>


我的HTML

<!-- lectures combo-->
<label for = "LectTitle">
<span>Select Lecture:</span>
&nbsp &nbsp &nbsp
<select name="lectures" onchange="showReport2(this.value)">
    <option value="">----Select Lecture----</option>
    <option value="1">Accounts</option>
    <option value="Web Design">Web Design</option>
    <option value="Database Administration">Database Administration</option>
    <option value="Mathematics">Mathematics</option>
    <option value="English">English</option>
    <option value="French">French</option>
    <option value="Mobile Device">Mobile Device</option>
</select>
</label>

<br/>
<hr>

<div id="txtHint">
    <center>
        Reports Will be displayed here
    </center>
</div> 


我的PHP脚本

<?php
$q = intval($_GET['q']);

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


$sql="
    SELECT 
        * 
    FROM 
        lecture 
    WHERE 
        LECTURE_NUMBER = '".$q."'
    ";

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

echo "
<table border='1'>
<tr>
    <th>Title</th>
    <th>Overview</th>
    <th>Room</th>
    <th>Organiser</th>
    <th>Presenter</th>
    <th>Date</th>
    <th>Time</th>
</tr>
";

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['TITLE'] . "</td>";
    echo "<td>" . $row['OVERVIEW'] . "</td>";
    echo "<td>" . $row['ROOM'] . "</td>";
    echo "<td>" . $row['ORGANISER'] . "</td>";
    echo "<td>" . $row['PRESENTER'] . "</td>";
    echo "<td>" . $row['DATE'] . "</td>";
    echo "<td>" . $row['TIME'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($connect);
?>

1 个答案:

答案 0 :(得分:0)

试试这个

while($row = $result->fetch_array())
{