Проблем при търсачка в база данни

systems

Registered
Добър вечер имам следната търсачка но проблема и е че вади всички резултати от базата с данни без да е натиснат бутона за търсене.

ето това е тя:
PHP:
<?php
	$conn = mysqli_connect("dbhost", "dbuser", "dbpass", "db");	
	$with_any_one_of = "";
	$with_the_exact_of = "";
	$without = "";
	$starts_with = "";
	$search_in = "";
	$advance_search_submit = "";
	
	$queryCondition = "";
	if(!empty($_POST["search"])) {
		$advance_search_submit = $_POST["advance_search_submit"];
		foreach($_POST["search"] as $k=>$v){
			if(!empty($v)) {

				$queryCases = array("with_any_one_of","with_the_exact_of","without","starts_with");
				if(in_array($k,$queryCases)) {
					if(!empty($queryCondition)) {
						$queryCondition .= " AND ";
					} else {
						$queryCondition .= " WHERE ";
					}
				}
				switch($k) {
					case "with_any_one_of":
						$with_any_one_of = $v;
						$wordsAry = explode(" ", $v);
						$wordsCount = count($wordsAry);
						for($i=0;$i<$wordsCount;$i++) {
							if(!empty($_POST["search"]["search_in"])) {
								$queryCondition .= $_POST["search"]["search_in"] . " LIKE '%" . $wordsAry[$i] . "%'";
							} else {
								$queryCondition .= "title LIKE '" . $wordsAry[$i] . "%' OR description LIKE '" . $wordsAry[$i] . "%'";
							}
							if($i!=$wordsCount-1) {
								$queryCondition .= " OR ";
							}
						}
						break;
					case "with_the_exact_of":
						$with_the_exact_of = $v;
						if(!empty($_POST["search"]["search_in"])) {
							$queryCondition .= $_POST["search"]["search_in"] . " LIKE '%" . $v . "%'";
						} else {
							$queryCondition .= "title LIKE '%" . $v . "%' OR description LIKE '%" . $v . "%'";
						}
						break;
					case "without":
						$without = $v;
						if(!empty($_POST["search"]["search_in"])) {
							$queryCondition .= $_POST["search"]["search_in"] . " NOT LIKE '%" . $v . "%'";
						} else {
							$queryCondition .= "title NOT LIKE '%" . $v . "%' AND description NOT LIKE '%" . $v . "%'";
						}
						break;
					case "starts_with":
						$starts_with = $v;
						if(!empty($_POST["search"]["search_in"])) {
							$queryCondition .= $_POST["search"]["search_in"] . " LIKE '" . $v . "%'";
						} else {
							$queryCondition .= "title LIKE '" . $v . "%' OR description LIKE '" . $v . "%'";
						}
						break;
					case "search_in":
						$search_in = $_POST["search"]["search_in"];
						break;
				}
			}
		}
	}
	$orderby = " ORDER BY id desc"; 
	$sql = "SELECT * FROM links " . $queryCondition;
	$result = mysqli_query($conn,$sql);
	
?>
<html>
	<head>
	<title>Advanced Search using PHP</title>
	<style>
		body{
			width: 600px;
			font-family: "Segoe UI",Optima,Helvetica,Arial,sans-serif;
			line-height: 25px;
		}
		.search-box {
			padding: 30px;
			background-color:#C8EEFD;
		}
		.search-label{
			margin:2px;
		}
		.demoInputBox {    
			padding: 10px;
			border: 0;
			border-radius: 4px;
			margin: 0px 5px 15px;
			width: 250px;
		}
		.btnSearch{    
			padding: 10px;
			background: #84D2A7;
			border: 0;
			border-radius: 4px;
			margin: 0px 5px;
			color: #FFF;
			width: 150px;
		}
		#advance_search_link {
			color: #001FFF;
			cursor: pointer;
		}
		.result-description{
			margin: 5px 0px 15px;
		}
	</style>
	<script>
		function showHideAdvanceSearch() {
			if(document.getElementById("advanced-search-box").style.display=="none") {
				document.getElementById("advanced-search-box").style.display = "block";
				document.getElementById("advance_search_submit").value= "1";
			} else {
				document.getElementById("advanced-search-box").style.display = "none";
				document.getElementById("with_the_exact_of").value= "";
				document.getElementById("without").value= "";
				document.getElementById("starts_with").value= "";
				document.getElementById("search_in").value= "";
				document.getElementById("advance_search_submit").value= "";
			}
		}
	</script>
	</head>
	<body>
		<h2>Advanced Search using PHP</h2>
    <div>      
			<form name="frmSearch" method="post" action="index.php">
			<input type="hidden" id="advance_search_submit" name="advance_search_submit" value="<?php echo $advance_search_submit; ?>">
			<div class="search-box">
				<label class="search-label">With Any One of the Words:</label>
				<div>
					<input type="text" name="search[with_any_one_of]" class="demoInputBox" value="<?php echo $with_any_one_of; ?>"	/>
					<span id="advance_search_link" onClick="showHideAdvanceSearch()">Advance Search</span>
				</div>				
				<div id="advanced-search-box" <?php if(empty($advance_search_submit)) { ?>style="display:none;"<?php } ?>>
					<label class="search-label">With the Exact String:</label>
					<div>
						<input type="text" name="search[with_the_exact_of]" id="with_the_exact_of" class="demoInputBox" value="<?php echo $with_the_exact_of; ?>"	/>
					</div>
					<label class="search-label">Without:</label>
					<div>
						<input type="text" name="search[without]" id="without" class="demoInputBox" value="<?php echo $without; ?>"	/>
					</div>
					<label class="search-label">Starts With:</label>
					<div>
						<input type="text" name="search[starts_with]" id="starts_with" class="demoInputBox" value="<?php echo $starts_with; ?>"	/>
					</div>
					<label class="search-label">Search Keywords in:</label>
					<div>
						<select name="search[search_in]" id="search_in" class="demoInputBox">
							<option value="">Select Column</option>
							<option value="title" <?php if($search_in=="title") { echo "selected"; } ?>>Title</option>
							<option value="description" <?php if($search_in=="description") { echo "selected"; } ?>>Description</option>
						</select>
					</div>
				</div>
				
				<div>
					<input type="submit" name="go" class="btnSearch" value="Search">
				</div>
			</div>
			</form>	
			<?php while($row = mysqli_fetch_assoc($result)) { ?>
			<div>
				<div><strong><?php echo $row["title"]; ?></strong></div>
				<div class="result-description"><?php echo $row["description"]; ?></div>
			</div>
			<?php } ?>
		</div>
	</body>
</html>




опитах с
PHP:
if (mysqli_num_rows($searchResults) > 0) {
while($row = mysqli_fetch_assoc($result))

но скрипта продължава да изкарва резултатите без да съм натиснал бутона за търсене.

това и е sql.

PHP:
CREATE TABLE `links` (
  `id` int(8) NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `votes` tinyint(2) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Въпроса ми е къде греша и изобщо може ли да се направи да изкарва резултат
само при търсене вместо да изкарва цялата база данни без да е натиснат бутона търсене.
 
PHP:
<?php
        $conn = mysqli_connect("dbhost", "dbuser", "dbpass", "db");     
        $with_any_one_of = "";
        $with_the_exact_of = "";
        $without = "";
        $starts_with = "";
        $search_in = "";
        $advance_search_submit = "";
        $result = false;
        $queryCondition = "";
        if(!empty($_POST["search"])) {
                $advance_search_submit = $_POST["advance_search_submit"];
                foreach($_POST["search"] as $k=>$v){
                        if(!empty($v)) {

                                $queryCases = array("with_any_one_of","with_the_exact_of","without","starts_with");
                                if(in_array($k,$queryCases)) {
                                        if(!empty($queryCondition)) {
                                                $queryCondition .= " AND ";
                                        } else {
                                                $queryCondition .= " WHERE ";
                                        }
                                }
                                switch($k) {
                                        case "with_any_one_of":
                                                $with_any_one_of = $v;
                                                $wordsAry = explode(" ", $v);
                                                $wordsCount = count($wordsAry);
                                                for($i=0;$i<$wordsCount;$i++) {
                                                        if(!empty($_POST["search"]["search_in"])) {
                                                                $queryCondition .= $_POST["search"]["search_in"] . " LIKE '%" . $wordsAry[$i] . "%'";
                                                        } else {
                                                                $queryCondition .= "title LIKE '" . $wordsAry[$i] . "%' OR description LIKE '" . $wordsAry[$i] . "%'";
                                                        }
                                                        if($i!=$wordsCount-1) {
                                                                $queryCondition .= " OR ";
                                                        }
                                                }
                                                break;
                                        case "with_the_exact_of":
                                                $with_the_exact_of = $v;
                                                if(!empty($_POST["search"]["search_in"])) {
                                                        $queryCondition .= $_POST["search"]["search_in"] . " LIKE '%" . $v . "%'";
                                                } else {
                                                        $queryCondition .= "title LIKE '%" . $v . "%' OR description LIKE '%" . $v . "%'";
                                                }
                                                break;
                                        case "without":
                                                $without = $v;
                                                if(!empty($_POST["search"]["search_in"])) {
                                                        $queryCondition .= $_POST["search"]["search_in"] . " NOT LIKE '%" . $v . "%'";
                                                } else {
                                                        $queryCondition .= "title NOT LIKE '%" . $v . "%' AND description NOT LIKE '%" . $v . "%'";
                                                }
                                                break;
                                        case "starts_with":
                                                $starts_with = $v;
                                                if(!empty($_POST["search"]["search_in"])) {
                                                        $queryCondition .= $_POST["search"]["search_in"] . " LIKE '" . $v . "%'";
                                                } else {
                                                        $queryCondition .= "title LIKE '" . $v . "%' OR description LIKE '" . $v . "%'";
                                                }
                                                break;
                                        case "search_in":
                                                $search_in = $_POST["search"]["search_in"];
                                                break;
                                }
                        }
                }
        $orderby = " ORDER BY id desc"; 
        $sql = "SELECT * FROM links " . $queryCondition;
        $result = mysqli_query($conn,$sql);
        }

        
?>
<html>
        <head>
        <title>Advanced Search using PHP</title>
        <style>
                body{
                        width: 600px;
                        font-family: "Segoe UI",Optima,Helvetica,Arial,sans-serif;
                        line-height: 25px;
                }
                .search-box {
                        padding: 30px;
                        background-color:#C8EEFD;
                }
                .search-label{
                        margin:2px;
                }
                .demoInputBox {    
                        padding: 10px;
                        border: 0;
                        border-radius: 4px;
                        margin: 0px 5px 15px;
                        width: 250px;
                }
                .btnSearch{    
                        padding: 10px;
                        background: #84D2A7;
                        border: 0;
                        border-radius: 4px;
                        margin: 0px 5px;
                        color: #FFF;
                        width: 150px;
                }
                #advance_search_link {
                        color: #001FFF;
                        cursor: pointer;
                }
                .result-description{
                        margin: 5px 0px 15px;
                }
        </style>
        <script>
                function showHideAdvanceSearch() {
                        if(document.getElementById("advanced-search-box").style.display=="none") {
                                document.getElementById("advanced-search-box").style.display = "block";
                                document.getElementById("advance_search_submit").value= "1";
                        } else {
                                document.getElementById("advanced-search-box").style.display = "none";
                                document.getElementById("with_the_exact_of").value= "";
                                document.getElementById("without").value= "";
                                document.getElementById("starts_with").value= "";
                                document.getElementById("search_in").value= "";
                                document.getElementById("advance_search_submit").value= "";
                        }
                }
        </script>
        </head>
        <body>
                <h2>Advanced Search using PHP</h2>
    <div>      
                        <form name="frmSearch" method="post" action="index.php">
                        <input type="hidden" id="advance_search_submit" name="advance_search_submit" value="<?php echo $advance_search_submit; ?>">
                        <div class="search-box">
                                <label class="search-label">With Any One of the Words:</label>
                                <div>
                                        <input type="text" name="search[with_any_one_of]" class="demoInputBox" value="<?php echo $with_any_one_of; ?>"  />
                                        <span id="advance_search_link" onClick="showHideAdvanceSearch()">Advance Search</span>
                                </div>                          
                                <div id="advanced-search-box" <?php if(empty($advance_search_submit)) { ?>style="display:none;"<?php } ?>>
                                        <label class="search-label">With the Exact String:</label>
                                        <div>
                                                <input type="text" name="search[with_the_exact_of]" id="with_the_exact_of" class="demoInputBox" value="<?php echo $with_the_exact_of; ?>"       />
                                        </div>
                                        <label class="search-label">Without:</label>
                                        <div>
                                                <input type="text" name="search[without]" id="without" class="demoInputBox" value="<?php echo $without; ?>"     />
                                        </div>
                                        <label class="search-label">Starts With:</label>
                                        <div>
                                                <input type="text" name="search[starts_with]" id="starts_with" class="demoInputBox" value="<?php echo $starts_with; ?>" />
                                        </div>
                                        <label class="search-label">Search Keywords in:</label>
                                        <div>
                                                <select name="search[search_in]" id="search_in" class="demoInputBox">
                                                        <option value="">Select Column</option>
                                                        <option value="title" <?php if($search_in=="title") { echo "selected"; } ?>>Title</option>
                                                        <option value="description" <?php if($search_in=="description") { echo "selected"; } ?>>Description</option>
                                                </select>
                                        </div>
                                </div>
                                
                                <div>
                                        <input type="submit" name="go" class="btnSearch" value="Search">
                                </div>
                        </div>
                        </form> 
                        <?php 
                        if($result != false):
                        while($row = mysqli_fetch_assoc($result)): ?>
                        <div>
                                <div><strong><?php echo $row["title"]; ?></strong></div>
                                <div class="result-description"><?php echo $row["description"]; ?></div>
                        </div>
                        <?php 
                        endwhile; 
                        endif; ?>
                </div>
        </body>
</html>

Така?

Добавих $result = false;
Сложих другата декларация на $result в if-a ако е постнато.
Доло проверявам ако $result не е false, тогава да fetch-ва.
 

Back
Горе