我对编码很陌生,并且一直收到这个错误,我不知道原因是什么……我试图从Mysql数据库中搜索/检索数据.这个想法是有人选择一个搜索类别(比如名字),然后输入一个名字,然后代码从数据库表Customers中检索所有相关的匹配。
我得到了以下错误:
致命错误:调用第36行./search.php中未定义的函数query()
有人能帮上忙吗。
<html>
<head>
<title>pageName</title>
<style type="text/css">
table {
background-color: #ADD8E6;
border: 1px solid black;
font-family: Arial; font-size: 14px;
}
th {
text-align: left;
}
</style>
</head>
<body>
<h1>MEMBERS SEARCH</h1>
<form method="post" action="search.php">
<input type="hidden" name="submitted" value="true"/>
<label> Search Category:
<select name="category">
<option value="firstname">First NAME</option>
<option value="lastname">Last NAME</option>
</select>
</label>
<label> Search Criteria:<input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])){
// connect to the DB
include('connect.php');
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM Customers WHERE $firstname LIKE '%" . $criteria ."%'";
$result = $query ($con, $query) or die ('error getting data from database');
$num_rows = mysql_num_rows ($result);
echo "$num_rows results found";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<table>
<tr>
<td width="300" ><font face="Arial Black" size="2"><?php echo $row['firstname']?> <?php echo $row['lastname']?></font></td>
</tr>
</table>
<table>
<tr>
?>
</body>
</html>发布于 2015-12-03 22:57:24
尝试将代码更改为如下所示。
<form method="post" action="search.php">
<input type="hidden" name="submitted" value="true"/>
<label> Search Category:
<select name="category">
<option value="firstname">First NAME</option>
<option value="lastname">Last NAME</option>
</select>
</label>
<label> Search Criteria:<input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])){
// connect to the DB
include('connect.php');
$category = mysqli_real_escape_string($con, $_POST['category']);
$criteria = mysqli_real_escape_string($con, $_POST['criteria']);
$query = "SELECT * FROM Customers WHERE firstname LIKE '%" . $criteria ."%'";
$result = mysqli_query($con, $query);
$num_rows = mysqli_num_rows($result);
echo "$num_rows results found";对变化的简要解释:
发布于 2015-12-03 22:26:57
您有$query而不是查询,它应该是一个函数,而不是变量。如果有更多的话,这可能会解决你的问题。
$result = $query ($con, $query) or die ('error getting data from database');应:
$result = query($con, $query) or die('error getting data from database');发布于 2015-12-03 22:28:54
此外,您还有查询错误:
$query = "SELECT * FROM Customers WHERE $firstname LIKE '%" . $criteria ."%'";应:
$query = "SELECT * FROM Customers WHERE firstname LIKE '%" . $criteria ."%'";应该是名字而不是$firstname。
也是
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {虽然循环没有结束括号,但应该如下所示:
<table>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>
<td width="300" ><font face="Arial Black" size="2"><?php echo $row['firstname']?> <?php echo $row['lastname']?></font></td>
</tr>
<table> <---- This is html mistake also remove it
<tr> <----- This is html mistake also remove it
<?php
}
?>
</table>我在循环中移动了表标记,因为这是从循环中生成行的逻辑方法,但是在一些例子中,您也可以循环整个表--您将在html代码中得到很多表。
https://stackoverflow.com/questions/34077354
复制相似问题