Задать вопрос
Ответы пользователя по тегу Веб-разработка
  • Пустая строка передается $_POST по умолчанию. Почему?

    ermolaevalexey
    @ermolaevalexey Автор вопроса
    frontend веб-разработчик
    function fetchFlats() {
    		global $link;
    		$where = '';
    		if(!empty($_POST['city'])){
    			if (count($_POST) > 1) {
    				$where .= 'rc.id_city = '.$_POST['city'].' ';
    			} else {
    				$where .= 'rc.id_city = '.$_POST['city'];
    			}
    		} else {//если пустой параметр то выкидываем
    			unset($_POST['city']);
    		}
    		if(!empty($_POST['metro']))  {
    			if (count($_POST) > 1) { //если параметров больше одного добавляем AND
    				$where .= 'AND rc.id_metro = '.$_POST['metro'].' ';
    			} else {//если один параметр
    				$where .= 'rc.id_metro = '.$_POST['metro'];
    			}
    		} else {
    			unset($_POST['metro']);
    		}	
    		if(  !empty($_POST['area_from']) ) {
    			if (count($_POST) > 1) {
    				$where .= 'AND f.total_area >= '.$_POST['area_from'].' ';
    			} else {
    				$where .= 'f.total_area >= '.$_POST['area_from'];
    			}
    		} else {
    			unset($_POST['area_from']);
    		}
    		if(  !empty($_POST['area_to']) ) {
    			if (count($_POST) > 1) {
    				$where .= 'AND f.total_area <= '.$_POST['area_to'].' ';
    			} else {
    				$where .= 'f.total_area <= '.$_POST['area_to'];
    			}
    		} else {
    			unset($_POST['area_to']);
    		}
    		if( !empty($_POST['price_from']) ) {
    			if (count($_POST) > 1) {
    				$where .= 'AND f.cost >= '.$_POST['price_from'].' ';
    			} else {
    				$where .= 'f.cost >= '.$_POST['price_from'];
    			}
    		} else {
    			unset($_POST['price_from']);
    		}
    		if( !empty($_POST['price_to']) ) {
    			if (count($_POST) > 1) {
    				$where .= 'AND f.cost <= '.$_POST['price_to'].' ';
    			} else {
    				$where .= 'f.cost <= '.$_POST['price_to'];
    			}
    		} else {
    			unset($_POST['price_to']);
    		}
    		$sql = "SELECT * 
    				FROM flat f
    				INNER JOIN building b ON f.id_building = b.id_building
    				INNER JOIN residential_complex rc ON b.id_residential_complex = rc.id_residential_complex WHERE ".$where;
    
    		if (!$result = mysqli_query($link,$sql)) {
    			echo $where;echo "<br/>";
    			var_dump($link);
    		} else {
    			$items = mysqli_fetch_all($result, MYSQLI_ASSOC);
    			mysqli_free_result($result);
    			return $items;
    		}
    	}


    Результат получается неожиданный:
    AND f.total_area >= 60  //распечатанный остаток строки запроса - переменная $where
    Ошибка синтаксиса MySql //(это ожидаемо, когда после WHERE идет сразу AND)
    //И самое главное - распечатанный пост:
    Array
    (
        [area_from] => 60
    )
    1 //кол-во параметров 1! Почему не выполняется вторая часть условия, и $where не идет      
       //без AND?

    Получается что один больше единицы, это бред какой-то или такой косячный код, но я уже не могу увидеть что именно не так, может быть Вы поможете еще разок?
    Ответ написан