猿问

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

我正在尝试使用不同的输入字段在 php mysql 中搜索数据,但我的代码似乎是正确的,但每当我尝试在页面内切换时都不会停留在会话中...


    <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>

我总是收到一个错误,说某些值未定义...


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

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

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

每次我尝试分页到下一页时,都没有定义上述变量...会话用于在我尝试移动到下一页时重置变量。


慕的地10843
浏览 118回答 1
1回答

犯罪嫌疑人X

您检查会话变量内容的条件不会按您预期的那样工作。您正在使用else if这意味着这if(isset($_SESSION['category']))是真的,其余的else if将不会被执行。会话仍被保存,但仅category到达(因为else)将您的代码更改为:if(isset($_POST['submit'])) {&nbsp; &nbsp; // your code}else{&nbsp; &nbsp; if(isset($_SESSION['category'])) {&nbsp; &nbsp; &nbsp; &nbsp; $category&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $_SESSION['category'];&nbsp; &nbsp; }&nbsp; &nbsp; if(isset($_SESSION['bedroom'])){&nbsp; &nbsp; &nbsp; &nbsp; $bedroom&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $_SESSION['bedroom'];&nbsp; &nbsp; }&nbsp; &nbsp; if(isset($_SESSION['price'])){&nbsp; &nbsp; &nbsp; &nbsp; $price&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $_SESSION['price'];&nbsp; &nbsp; }&nbsp; &nbsp; if(isset($_SESSION['location'])){&nbsp; &nbsp; &nbsp; &nbsp; $location&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $_SESSION['location'];&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答