使用php搜索多个字段的代码

时间:2016-06-09 08:49:09

标签: php

我是非常非常新的PHP和MySQL,所以请你好。我创建了一个表单,用户可以在其中搜索房价,卧室和地点,结果取决于用户填写表单的方式。例如,用户填写字段2卧室房价4500和地点将选择任何位置。为此我写了这个:

    <?php
        require '../connection/config.php';
        if(isset($_POST['submit'])) {   
            $rate=$_POST['rate'];
            $bedroom=$_POST['bedroom'];
            $place=$_POST['place'];
    $sql = "SELECT * FROM villa WHERE rate LIKE '%" . $_POST['rate'] . "%'
               AND bedroom LIKE '%" . $_POST['bedroom'] . "%'
               AND place LIKE '%" . $_POST['place'] . "%'";

      if($place=="any" && $rate==$rate && $bedroom==$price)
      {
          $sql="select * from villa where rate='$rate' and bedroom='$bedroom'";
      }
               $result=mysqli_query($conn,$sql);
                while($row = mysqli_fetch_array($result)) {
                if($result)
              {

            echo $row['description'] . "<br />";
            ?><td><img src="../uploads/post/<?php echo $row['image']; ?>" style='width:100px;'><br><br>
            <?php
              }
        else
            echo "error";
    }
    }
?>


    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <tr>
            <td>rate:</td>
            <td><select name="rate" />
            <option value="select">select</option>
            <option value="125">125</option>
            <option value="357">357</option>
            <option value="500">500</option></select>
            </td>
        </tr>
        <tr>
            <td>bedrooms:</td>
            <td><select name="gender" />
            <option value="select">select</option>
            <option value="3">3</option>
            <option value="6">6</option>
            <option value="2">2</option>
            </select>
            </td>
        </tr>
        <tr>
            <td>place:</td>
            <td><select name="place" />
            <option value="any">any</option>
            <option value="kannur">kannur</option>
            <option value="calct">calicut</option>
            <option value="tly">telicherry</option>
            </select>
            </td>
        </tr>

            <td>&nbsp;</td>
            <td><input type="submit" name="submit" value="Search" /></td>
        </tr>
    </form>
</table>

1 个答案:

答案 0 :(得分:1)

#1

$bedroom

错误导致ur变量被称为$sql="select * from villa where rate='$rate' and bedroom='$bedroom'";

<tr>
    <td>bedrooms:</td>
    <td><select name="gender" />
    <option value="select">select</option>
    <option value="3">3</option>
    <option value="6">6</option>
    <option value="2">2</option>
    </select>
    </td>
</tr>

#2

<select name="gender" />

你选择NAME错误<?php require '../connection/config.php'; if(isset($_POST['submit'])) { $rate=$_POST['rate']; $bedroom=$_POST['bedroom']; $place=$_POST['place']; $sql = "SELECT * FROM villa WHERE rate LIKE '%" . $_POST['rate'] . "%' AND bedroom LIKE '%" . $_POST['bedroom'] . "%' AND place LIKE '%" . $_POST['place'] . "%'"; if($place=="any" && $rate==$_POST['rate'] && $bedroom==$_POST['bedroom']) { $sql="select * from villa where rate='$rate' and bedroom='$bedroom'"; } $result=mysqli_query($conn,$sql); while($row = mysqli_fetch_array($result)) { if($result) { echo $row['description'] . "<br />"; ?><td><img src="../uploads/post/<?php echo $row['image']; ?>" style='width:100px;'><br><br><?php } else { echo "error"; } } } ?> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <tr> <td>rate:</td> <td><select name="rate" /> <option value="select">select</option> <option value="125">125</option> <option value="357">357</option> <option value="500">500</option></select> </td> </tr> <tr> <td>bedrooms:</td> <td><select name="bedroom" /> <option value="select">select</option> <option value="3">3</option> <option value="6">6</option> <option value="2">2</option> </select> </td> </tr> <tr> <td>place:</td> <td><select name="place" /> <option value="any">any</option> <option value="kannur">kannur</option> <option value="calct">calicut</option> <option value="tly">telicherry</option> </select> </td> </tr> <td>&nbsp;</td> <td><input type="submit" name="submit" value="Search" /></td> </tr> </form> </table> 它也必须是卧室。 在这里你完整的代码。

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
      switch(type) {
             case NSFetchedResultsChangeUpdate:
             //you might consider ignoring it but I wont prefer that though :)
             break;
      }
}