Answer the question
In order to leave comments, you need to log in
An empty string is passed to $_POST by default. Why?
I'm very, very asking for help. Any assumptions and hypotheses are accepted / I help friends to make a project. The essence is that:
On the filled form fields on the page selection in a DB becomes and results get.
The script is executed right there on the page. On submit, the POST method on the page calls the fetchFlats() function, which returns an array of values from the database.
There was a problem - the form cannot be checked for fullness - text inputs are filled with empty lines every time the function is called. That is, nothing has been entered into the fields yet, but an array with data comes. Additional Header did not solve the problem.
Please tell me how to get rid of these empty lines.
Very, very urgent. Thanks in advance
index.php is where the action happens:
<?php
//Подключение библиотек
require "inc/db.inc.php";
require "inc/lib.inc.php";
//header('Content-Type:text/html;charset=utf-8');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>База</title>
</head>
<body>
<div>
<fieldset legend="Укажите парпаметры выбора">
<form action="" method="post">
<select name="city">
<option selected disabled>Выберите город</option>
<?
$cities = selectCity();
foreach ($cities as $city) {?>
<option value="<?=$city['ID_City']?>"><?=$city['Name']?></option>
<?}?>
</select>
<select name="metro">
<option selected disabled>Выберите метро</option>
<?
$metros = selectMetro();
foreach ($metros as $metro) {?>
<option value="<?=$metro['ID_Metro']?>"><?=$metro['Name']?></option>
<?}?>
</select>
<label for="area">
Общая площадь
<input type="number" name="area_from" min="30" max="150"/>
<input type="number" name="area_to" min="30" max="150"/>
</label>
<label for="price">
Цена
<input type="number" name="price_from"/>
<input type="number" name="price_to"/>
</label>
<button type="submit">Показать варианты</button>
</form>
</fieldset>
<table border="1" cellpadding="5" cellspacing="0" width="100%">
<tr>
<?
if(empty($_POST) ){
echo 'Введите данные для поиска!';
} else {
$flats = fetchFlats();
echo "<pre>";
var_dump($flats);
print_r($_POST);
echo "</pre>";
echo count($_POST);
}
?>
</tr>
</table>
</div>
</body>
</html>
function fetchFlats() {
global $link;
$where = '';
if(isset($_POST['city'])){
if (count($_POST) >= 1) {
$where .= 'rc.id_city = '.$_POST['city'].' ';
} else {
$where .= 'rc.id_city = '.$_POST['city'];
}
}
if(isset($_POST['metro'])){
if (count($_POST) >= 1) {
$where .= 'AND rc.id_metro = '.$_POST['metro'].' ';
} else {
$where .= 'rc.id_metro = '.$_POST['metro'];
}
}
if(isset($_POST['area_from']) && isset($_POST['area_to'])){
if (count($_POST) >= 1) {
$where .= 'AND f.total_area >= '.$_POST['area_from'].' AND f.total_area <= '.$_POST['area_to'].' ';
} else {
$where .= 'f.total_area >= '.$_POST['area_from'].' AND f.total_area <= '.$_POST['area_to'];
}
}
if(isset($_POST['price_from']) && isset($_POST['price_to'])){
if (count($_POST) >= 1) {
$where .= 'AND f.cost >= '.$_POST['price_from'].' AND f.cost <= '.$_POST['price_to'].' ';
} else {
$where .= 'f.cost >= '.$_POST['price_from'].' AND f.cost <= '.$_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;
} else {
$items = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
return $items;
}
}
Array
(
[area_from] =>
[area_to] =>
[price_from] =>
[price_to] =>
)
var_dump
makes it clear that an empty string has crept in at the place of the values, respectively, $_POST is never empty. I have already broken my whole brain, thinking how to overcome it. I really, really need your help. Thanks again.
Answer the question
In order to leave comments, you need to log in
Enter something in the area_* or price_* fields, click "Show Options".
The form will submit and the values will be there.
Try to open a new page with this address - you will see that $_POST will be empty.
After that, you can exhale, relax, and go to read the textbook.
PS: And yes, at the expense of "urgently needed", you are in vain. All this will have to be either thrown away later, or strongly, strongly rewritten, because there are more holes in this code than in the hat of the postman Pechkin. Any ten-year-old hacker will make your site at-at-at.
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?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question