我真的很陌生,正在从事这项作业,我应该在数据库中为作者创建一个下拉列表,并且当用户将鼠标悬停在其中一位作者上时,他们所写的书会显示出来在同一页面上的某个区域中,现在我尝试了所有已知的操作,但是仍然出现此错误。
警告:第21行的C:\ xampp \ htdocs \ demo \ books.php中遇到的非数字值
即使我数据库中的数据全部为文本,也不需要任何数值。
我就是这样提交作业的,但我仍然想知道我做错了什么,我将非常感谢您的帮助
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "books";
// variables used in script
$Author =$_REQUEST["Author"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT BookName, BookID FROM `books`"+ 'WHERE AuthorName='+ '"'+'$Author'+'"';
$sqlData= mysqli_query($conn, $sql);
if($sqlData)
{
while($row =mysqli_fetch_assoc($sqlData)){
echo" <table style='width:100%'>"+ "<tr> <th>BookName</th> <th>BookID</th> <th>AuthorName</th> </tr>"+"<tr> <td>"+ $row[BookName] +
"</td><td>"+$row[BookID] +"</td> </tr>";
}
echo "</table>" ;
}
?>
这是我的下拉列表所在的整个代码
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Books</title>
<style >
label { width: 5em; float: left; }
</style>
<script >
function ShowBook_Info(Author) {
request=new XMLHttpRequest();
request.onreadystatechange=function() {
if (request.readyState==4 && request.status==200) {
document.getElementById("books").innerHTML=request.responseText;
}
}
request.open("GET","books.php?Author="+Author,true);
request.send();
}
</script>
</head>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "books";
$sql="SELECT AuthorName FROM `books`";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sqlData= mysqli_query($conn, $sql);
if($sqlData){
echo "<select onchange='ShowBook_Info(this.value)'>" ;
while($row =mysqli_fetch_assoc($sqlData)){
$Value=$row["AuthorName"];
echo "<option value=";
echo '$value'.'">';
echo $row["AuthorName"];
echo "</option>";
}
echo "</select>" ;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
?>
<body onload="ShowBook_Info('Harper Lee');" >
<div style="color:blue;" id='books'>
This is a test text
</div>
</form>
</body>
</html>
答案 0 :(得分:-1)
您的代码中有很多错误。 请检查php的语法,以更好地了解php上的编码
$sql = "SELECT BookName, BookID FROM
本书"+ 'WHERE AuthorName='+ '"'+'$Author'+'"';
AuthorName='+ '"'+'$Author'+'"';
不正确。只是简单的使用。
`AuthorName ='{$ Author}'“;完整的内容将是:
$sql = "SELECT BookName, BookID FROM
书籍`WHERE AuthorName ='{$ Author}'“;
$row[BookName]
=> $row['BookName']
或$row["BookName"]