从数据库mysql中检索数据不起作用
retrieve data from database mysql not working
嗨,我正在使用ajax 使用html下拉列表的onchange事件
在我使用的代码中,当我更改下降。
但它不起作用。可能出了什么问题?
这是代码
<html>
<head>
<script>
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", "getuser.php?q=" + str, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<?php
mysql_connect('localhost', 'tiger', 'tiger');
mysql_select_db('theaterdb');
$sql = "select theater_name from theater;";
$result = mysql_query($sql);
echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>";
while ( $row = mysql_fetch_array( $result ) ) {
echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>";
}
echo "</select>";
?>
</form>
<br>
<div id="txtHint"><b>Info</b></div>
</body>
</html>
getuser.php 的代码
<?php
$q = $_GET["q"];
$con = mysqli_connect("localhost", "tiger", "tiger", "theaterdb");
if ( !$con ) {
die('Could not connect: ' . mysqli_error( $con ) );
}
mysqli_select_db( $con );
$sql = "SELECT address FROM theater WHERE theater_name = '".$q."'";
$result = mysqli_query( $con, $sql );
echo "<table border='1'>
<tr>
<th>Firstname</th>
</tr>";
while( $row = mysqli_fetch_array( $result ) ) {
echo "<tr>";
echo "<td>" . $row['address'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
好吧,我稍微调整了一下你的文件,你不应该再使用mysql_或mysqli_函数了,只是不要。。。当然,您不应该在一个文件中使用mysql函数,而在另一个文件使用mysqli函数。。。我已经将它们转换为使用PDO,您的脚本现在不易受SQL注入的影响,而且据我所知,它工作得很好。
index.html
<html>
<head>
<script>
function showUser(str) {
if(str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if(window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "SELECT theater_name FROM theater;";
$sth = $dbh->prepare($sql);
$sth->execute();
echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>";
}
echo "</select>";
?>
</form>
<br>
<div id="txtHint"><b>Info</b></div>
</body>
</html>
getuser.php
<?php
$q = strtolower(trim($_GET["q"]));
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = 'SELECT address FROM theater WHERE LOWER(theater_name) = :q';
$sth = $dbh->prepare($sql);
$sth->bindValue(':q', $q);
$sth->execute();
echo "<table border='1'><tr><th>Firstname</th></tr>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['address'] . "</td>";
echo "</tr>";
}
echo "</table>";
$dbh = null;
我不明白为什么你在两个php文件中都连接了数据库?我建议你访问下面的链接。
http://www.w3schools.com/php/php_ajax_database.asp
相关文章:
- 发送的 Ajax 数据不起作用
- 尝试使用 AngularJS ng-repeat 显示 JSON 数据不起作用
- Jquery/Ajax-formPost数据不起作用
- 从d3.js中的文件读取json数据不起作用
- 从服务器获取开机自检的数据不起作用
- 从角度到节点的 POST 数据不起作用
- 从 PHP 使用 D3.json() 获取 JSON 数据不起作用
- jQuery清空文本区域然后附加数据不起作用
- 使用 JSON 从 PHP 查询中获取数据不起作用
- AngularJS向表单传递数据不起作用
- ng angularjs中的重复过滤器数据不起作用
- Rickshaw:多个系列的数据不起作用
- 放置图像数据不起作用检查现有问题
- 从数据库mysql中检索数据不起作用
- 使用JQuery UI自动完成从JSON搜索数据不起作用
- SoundCloud:更新曲目元数据不起作用
- 通过Ajax上传文件使用append()附加数据不起作用
- Jquery的HTML数据不起作用
- Onclick事件只对第一个产品起作用,对其他获取的数据不起作用
- 在Android/iOS浏览器上提交隐藏字段数据不起作用