Danny830x Posted October 18, 2010 Share Posted October 18, 2010 Hi. I have a database named 'Forums' with a table named 'forumlist'. The fields in the table are 'id', 'name', 'link', and 'keys'. I am trying to search the database in either the 'name' or 'keys' field and display the matches. I am getting this error: Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\xampp\htdocs\search3.php on line 51 (The define fields) <html> <head> <title>My Forum Search</title> <style type="text/css"> table { background-color: #CCC } th { width: 150px; text-align: left; } </style> </head> <body> <h1>Forum Search</h1> <form method="post" action="search3.php"> <input type="hidden" name="submitted" value="true" /> <label>Search Category: <select name="category"> <option value="keys">Keyword</option> <option value="name">Name</option> </select> </label> <label><input type="text" name="criteria" /></label> <input type="submit" /> </form> <?php if (isset($_POST['submitted])) { DEFINE ('DB_USER', 'root'); DEFINE ('DB_PSWD', '******'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'Forums'); $dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME); $category = $_POST['category']; $criteria = $_POST['criteria']; $query = "SELECT * FROM forumlist WHERE $category LIKE '$criteria'"; $result = mysqli_query($dbcon, $query) or die ('Error retrieving data') echo "<table>"; echo "<tr> <th>Name</th><th>Link</th> </tr>"; while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { echo "<tr><td>"; echo $row['name']; echo "</td><td>"; echo $row['link']; echo "</td></tr>"; } echo "</table>"; } // end of main if state ?> </body> </html> Thanks in advance Link to comment https://forums.phpfreaks.com/topic/216134-search-mysql-database-coding-problem/ Share on other sites More sharing options...
Danny830x Posted October 18, 2010 Author Share Posted October 18, 2010 Also, I only want to display the 'name' and 'link' fields in the search results Link to comment https://forums.phpfreaks.com/topic/216134-search-mysql-database-coding-problem/#findComment-1123239 Share on other sites More sharing options...
Pikachu2000 Posted October 18, 2010 Share Posted October 18, 2010 if (isset($_POST['submitted])) { <-- missing a single quote before closing square bracket. Link to comment https://forums.phpfreaks.com/topic/216134-search-mysql-database-coding-problem/#findComment-1123240 Share on other sites More sharing options...
Pikachu2000 Posted October 18, 2010 Share Posted October 18, 2010 Also, I only want to display the 'name' and 'link' fields in the search results It looks like that's all that will be echoed from looking at your while loop. Link to comment https://forums.phpfreaks.com/topic/216134-search-mysql-database-coding-problem/#findComment-1123242 Share on other sites More sharing options...
Danny830x Posted October 18, 2010 Author Share Posted October 18, 2010 Thanks for the fix. That was really dumb of me, it was late and I just needed a fresh set of eyes I suppose. Now that the code works... I have it default to search by "Keyword" from database section 'keys', and the ability to change it to "Name" from database section 'name'. When I search from Keyword, it never returns results, but when I search from Name it works correctly. Any idea what the problem is there? Thanks again Link to comment https://forums.phpfreaks.com/topic/216134-search-mysql-database-coding-problem/#findComment-1123581 Share on other sites More sharing options...
BlueSkyIS Posted October 18, 2010 Share Posted October 18, 2010 perhaps expand your LIKE: $query = "SELECT * FROM forumlist WHERE $category LIKE '%$criteria%'"; Link to comment https://forums.phpfreaks.com/topic/216134-search-mysql-database-coding-problem/#findComment-1123583 Share on other sites More sharing options...
Danny830x Posted October 18, 2010 Author Share Posted October 18, 2010 Thanks but I gave that a try with your exact line, and still I receive the error code I created, "Error retrieving data." This is only for the "Keyword" option, the "Name" option works correctly. Strange Link to comment https://forums.phpfreaks.com/topic/216134-search-mysql-database-coding-problem/#findComment-1123584 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.