为什么我的文本框没有用查询结果更新
Why are my textboxes not updated with results from query
在我正在使用的书的章节末尾自学PHP使用问题,以学习如何应用每一章的方法。创建了一个html页面,该页面接受用户输入的汽车id,单击按钮,该汽车的信息就会显示在文本框中。我搜索了很多文档、SO和网络,但根据我目前的学习晴雨表,找不到我想要的方法。使用phpexplode()方法行得通吗?还是使用ajax/json做得更好?
当我点击搜索时,文本框中没有填充任何内容,所以如果有人能教我做错了什么,以及我需要在哪里修复代码,那将是非常棒的。谢谢
<?php
$host = "localhost";
$dbname = "car_search_db";
$user = "pontiac";
$password = "Race_Day!";
try {
$db = new PDO("mysql:dbname=$dbname;host=$host",$user,$password);
}
catch (PDOException $e) {
$error_message = $e->getMessage();
include ('error.php');
exit();
}
$car_ID = isset($_GET['car_ID']) ? $_POST['car_ID'] : '';
if($car_ID == "")
die("Invalid input");
$query = 'SELECT carID, make, model, year FROM fastCars
WHERE carID = :car_ID';
$statement = $db->prepare($query);
$statement->bindValue(':car_ID', $car_ID);
$statement->execute();
$cars = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($cars);
echo $json;
?>
<!DOCTYPE html>
<head>
<title>Fast Car Page</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<script type="text/javascript">
function queryCars() {
//get user input
var id = document.getElementById('car_ID').value;
//create a query string
var queryString = "?car_ID=" + car_ID;
//create XMLHTTP Request
var ajaxRequest;
try {
ajaxRequest = new XMLHttpRequest;
}catch(e) {
//IE Browsers
try {
ajaxRequest = new ActiveXobjectect("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXobjectect("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Browser not compatible");
return false;
}
}
}
//create ajax function that processes server response
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
var object = JSON.parse(xmlhttp.responseText);
document.getElementById('car_id').value = object[0].car_ID;
document.getElementById('make').value = object[1].make;
document.getElementById('model').value = object[2].model;
document.getElementById('year').value = object[3].year;
}
};
//execute the query
xmlhttp.open("POST", "car_search.php" + queryString, true);
xmlhttp.send();
}
</script>
<header><h1>Fast Car Lookup - Search & Split</h1></header>
<br>
<h2>Search for a car info by ID</h2>
<br>
<form action="car_search.php" method="get">
<label>StudentID:</label>
<input type="text" id="id" name="car_ID">
<br>
<br>
<input type="button" onclick="queryCars();" value="Search" id="get_info">
<h2>information will be displayed in the textboxes below for the car ID chosen:</h2>
<br>
<table id="outuput">
<tr>
<td><label>Car ID:</label></td>
<td><input type="text" id="car_ID" value="" readonly></td>
</tr>
<tr>
<td><label>Make:</label></td>
<td><input type="text" id="make" value="" readonly></td>
</tr>
<tr>
<td><label>Model:</label></td>
<td><input type="text" id="model" value="" readonly></td>
</tr>
<tr>
<td><label>Year:</label></td>
<td><input type="text" id="" value="" readonly></td>
</tr>
<br>
</table>
</form>
<a href="index.htm">Back to first page</a>
</body>
</html>
您在此处绑定了错误的变量:
$statement->bindValue(':car_ID', $student_ID);
应该是
$statement->bindValue(':car_ID', $car_ID);
同样,您在php中使用post,但car id在查询字符串中——应该在php中用GET。
相关文章:
- 如何将这些查询结果放入表中
- 如何在下拉列表更改时自动填充mysql查询结果中的文本框值
- 将数据库中的超链接添加到查询结果
- 为什么我的文本框没有用查询结果更新
- 使用jquery显示mysql查询结果
- 如何使用 angularjs 以表格格式显示 parse.com 的查询结果
- Meteor:在模板接受Mongo查询结果之前修改它的最佳实践
- 未捕获的范围错误:WebSQL查询结果中的项索引超出范围错误
- 表单提交后保留数据库查询结果
- 需要循环遍历一系列mysql查询结果
- OrientDB:在服务器端函数中访问查询结果数据
- Firebase 删除查询结果
- MongoDB查询结果作为循环控制器
- j查询结果不显示,除非我按“后退”按钮
- J查询结果显示问题
- 为什么脚本 Db 查询结果对象不同
- 从包含的 php 文件刷新查询结果
- 实现我的 Javascript 函数到数据库查询结果
- 通过输入键(不起作用)或搜索按钮(工作)查询结果
- 从 JSON(db 查询结果)加载大型 javascript 数组而不会使页面崩溃