0

I want to make dynamic search button i have this code but doesnt work it gives me this error
Could not connect to database. Fatal error: Wrong SQL: SELECT * FROM kladilnica WHERE Location = Macedonia AND Uplata = 50 AND Liked = 50 Error: Unknown column 'Macedonia' in 'where clause' in C:\xampp\htdocs\bootstrap\shared.php on line 238
Here is the code

if(isset($_POST['searchBtn'])) {
                            $selectCountryBox = $_POST['selectCountryBox'];
                            $filterByBet = $_POST['filterByBet'];
                            $filterByLikes = $_POST['filterByLikes'];
                            $filterByPublisher = $_POST['filterByPublisher'];
                            //$filterByDate = $_POST['filterByDate'];

                            $searchArr = array();
                            if($selectCountryBox != "") $searchArr[] = "Location = {$selectCountryBox}";
                            if($filterByBet != "") $searchArr[] = "Uplata = {$filterByBet}";
                            if($filterByLikes != "") $searchArr[] = "Liked = {$filterByLikes}";
                            if($filterByPublisher != "") $searchArr[] = "Username = {$filterByPublisher}";
                            //if($filterByDate != "") $searchArr[] = "Date = {$filterByDate}";

                            $searchArr = implode(" AND ", $searchArr);

                            $query = "SELECT * FROM kladilnica WHERE {$searchArr}";
                            $result = $conn->query($query);
                            if($result === false) {
                                echo "<b>Could not connect to database.</b>";
                                trigger_error('Wrong SQL: ' . $query . ' Error: ' . $conn->error, E_USER_ERROR);
                            } else {
                                echo "YES!";
                                echo $result;
                                /*while($row = $result->fetch_assoc()) {
                                    $getCountry = $row["Location"];
                                }*/
                            }
                        }
2
Contributors
1
Reply
13
Views
1 Year
Discussion Span
Last Post by hericles
1

You need to put quotes around the value for the Location column.

SELECT * FROM kladilnica WHERE Location = 'Macedonia' AND Uplata = 50 AND Liked = 50

So you need this:
if($selectCountryBox != "") $searchArr[] = "Location = '{$selectCountryBox}'";

Edited by hericles

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.