我正在创建一个小型模拟库管理员面板。 我是php的新手,所以希望有人可以帮助我的代码。
我正在尝试根据选择的下拉选项/ paremter来过滤结果。下拉列表指的是我的数据库表中的三列,其中需要提取所需信息。
所以例如:- book_title selected - search book_title WHERE query? IS LIKE '%';
我知道这是沿着这些方向但是想要一些指导。
<?php
// Ignore warning and error messages
error_reporting(E_ALL);
ini_set('display_errors', '1');
$username="";
$password="";
$database="books";
$value= $_POST["filter-query"];
mysql_connect(localhost,$username,$password);
@mysql_select_db($duncan_library) or die( "Unable to select database");
//catalog1//
if ($_POST['filter-query'] == 'catalog_number')
{
$query = "SELECT * FROM books WHERE catalog_number LIKE '%user-search'%;
}
elseif ($_POST['filter-query'] == 'book_title')
{
$query = "SELECT * FROM books WHERE book_title LIKE '%user-search'%;
}
elseif ($_POST['filter-query'] == 'book_author')
{
$query = "SELECT * FROM books WHERE book_author LIKE '%user-search'%;
}
$sql = mysql_query($query);
while ($row = mysql_fetch_array($sql)) {
$catalog_number = $row["catalog_number"];
$book_title = $row["book_title"];
$book_author = $row["book_author"];
echo $row["myrow"]."";
}
}
?>
Filter:<br>
<select name="filter-query"">
<option value="catalog_number">Catalog Number</option>
<option value="book_title">Book Title</option>
<option value="book_author">Book Author</option>
</select>
</select>
</form>
当前错误
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\library\index.php on line 20
答案 0 :(得分:0)
将LIKE字符串放在引号内,即使是%符号,也错过了双引号
//catalog1//
if ($_POST['filter-query'] == 'catalog_number')
{
$query = "SELECT * FROM books WHERE catalog_number LIKE '%user-search%'";
}
elseif ($_POST['filter-query'] == 'book_title')
{
$query = "SELECT * FROM books WHERE book_title LIKE '%user-search%'";
}
elseif ($_POST['filter-query'] == 'book_author')
{
$query = "SELECT * FROM books WHERE book_author LIKE '%user-search%'";
}