douyao2529 2019-05-15 14:01
浏览 51
已采纳

我正在尝试使用不同的输入来搜索我的数据库中的数据并保持所有会话

I am trying to search data in php mysql using different input fields but my code seems correct but doesn't stay in session whenever i try to switch within pages...

    <form method="POST" action="search_all_properties">
   <div class="form-row align-items-center">
    <div class="col-auto my-1">
      <select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="category" value="category" >
        <option selected>Category</option>
        <option value="houses">Houses</option>
        <option value="shops">Shops</option>
        <option value="office_spaces">Office Spaces</option>
      </select>
    </div>
    <div class="col-auto my-1">
      <select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="bedroom">
        <option selected>Bedrooms</option>
        <option value="1_bedroom">1 Bedroom</option>
        <option value="2_bedroom">2 Bedrooms</option>
        <option value="3_bedroom">3 Bedrooms</option>
      </select>
    </div>
    <div class="col-auto my-1">
      <select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="price">
        <option selected>Price</option>
        <option value="200,000">200,000</option>
        <option value="300,000">300,000</option>
        <option value="500,000">500,000</option>
        <option value="800,000">800,000</option>
        <option value="1_million">1 Million</option>
      </select>
    </div>
    <div class="col">
      <input type="text" onchange="trim(this)" name="location" class="form-control" placeholder="Location">
    </div>
    <div class="col-auto my-1">
      <button type="submit" name="submit" class="btn bg-info">Search</button>
    </div>
  </div>
        </form>

i always get an error that some values are not defined...

$bedroom  = $database->escape_value($bedroom);
        $price    = $database->escape_value($price);
        $location = $database->escape_value($location); 

The above variables are not defined each time i try to paginated to next page... session is for the variables are reset once i try to move to next page.

<?php
        if(isset($_POST['submit']) || isset($_SESSION['search'])){
        // 1. the current page number ($current_page)
        $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
        // 2. records per page ($per_page)
        $per_page = 2;
        // 3. total record count ($total_count)
        if(isset($_POST['submit'])) {
            $_SESSION['category']  = $_POST['category'];
            $category              = $_POST['category'];
            $_SESSION['bedroom']   = $_POST['bedroom'];
            $bedroom               = $_POST['bedroom']; 
            $_SESSION['price']     = $_POST['price'];
            $price                 = $_POST['price'];
            $_SESSION['location']  = $_POST['location'];
            $location              = $_POST['location'];
        } else { if(isset($_SESSION['category'])){
            $category         = $_SESSION['category'];
               }else if(isset($_SESSION['bedroom'])){
            $bedroom          = $_SESSION['bedroom'];
        }else if(isset($_SESSION['price'])){
            $price            = $_SESSION['price'];
        }else if(isset($_SESSION['location'])){
            $location         = $_SESSION['location'];
        }
        }  
        $category = $database->escape_value($category);
        $bedroom  = $database->escape_value($bedroom);
        $price    = $database->escape_value($price);
        $location = $database->escape_value($location); 

        $sql  = "SELECT COUNT(*) FROM pictures WHERE title LIKE '%$category%'";
        $sql .= " OR location LIKE '%$location%'";
        $sql .= " OR price LIKE '%$price%'";
        $sql .= " OR description LIKE '%$category%'";   
        $sql .= " OR bedroom LIKE '%$bedroom%'";
        $sql = $database->query($sql);
        $sql = $database->fetch_array($sql);
        $total_count = array_shift($sql);

        // Find all photos
        // use pagination instead
        $pagination = new Pagination($page, $per_page, $total_count);
        // Instead of finding all records, just find the records

        $sql  = "SELECT * FROM pictures WHERE title LIKE '%$category%'";
        $sql .= " OR location LIKE '%$location%'";
        $sql .= " OR price LIKE '%$price%'";    
        $sql .= " OR description LIKE '%$category%'";
        $sql .= " OR bedroom LIKE '%$bedroom%'";    
        $sql .= " ORDER by id DESC";
        $sql .= " LIMIT {$per_page}";
        $sql .= " OFFSET {$pagination->offset()}";
        $photos = Picture::find_by_sql($sql);
        $tcount = count($photos);   
        }
?>
  • 写回答

1条回答 默认 最新

  • duanji1899 2019-05-15 14:10
    关注

    Your condition to check the contents of the session variable won't work as you expect.

    You are using else if which mean that if(isset($_SESSION['category'])) is true, the remaining of the else if won't be executed.

    The session is still saved, but only category is reached (because of the else)

    Change your code to :

    if(isset($_POST['submit'])) {
        // your code
    }
    else
    {
        if(isset($_SESSION['category'])) {
            $category         = $_SESSION['category'];
        }
        if(isset($_SESSION['bedroom'])){
            $bedroom          = $_SESSION['bedroom'];
        }
        if(isset($_SESSION['price'])){
            $price            = $_SESSION['price'];
        }
        if(isset($_SESSION['location'])){
            $location         = $_SESSION['location'];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)