filtering results based on dropdown paramenters us

2019-09-12 05:23发布

I'm creating a small mock-library adminstrator panel. I'm new to php so was hoping someone out there can help with my code.

I'm trying to filter results based on whatever dropdown option/paremter is selected. The dropdowns refer to three columns within my database table where the required information is due to be pulled from.

So for instance: - book_title selected - search book_title WHERE query? IS LIKE '%'; I know it's along these lines but would like some guidance.

<?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>

Current Error

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

标签: php mysql mysqli
1条回答
够拽才男人
2楼-- · 2019-09-12 06:19

Put your LIKE string inside quotes even the % sign and also you missed closing double quotes

//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%'";
    }
查看更多
登录 后发表回答