我们正在尝试使用7个搜索条件为具有8个属性的数据库执行搜索表单。但我们只希望一次搜索一个事件。这是我到目前为止的代码,并希望将搜索到的信息显示在表中。任何知道在哪里寻找的帮助都会受到赞赏。
<?php
include 'database_connector.php';
if(isset($_POST['submit'])){
$type = $_POST['type'];
$team1 = $_POST['team1'];
$team2 = $_POST['team2'];
$place = $_POST['place'];
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$price = $_POST['price'];
$date = $year.'-'.$month.'-'.$day;
if($type)(
$result=mysqli_connect($con, "select * from Sports where `Event Type` = '$type'")
);
if($team1)(
$result1=mysqli_connect($con, "select * from Sports where `Team 1` = '$team1'")
);
if($team2)(
$result2=mysqli_connect($con, "select * from Sports where `Team 2` = '$team2'")
);
if($place)(
$result3=mysqli_connect($con, "select * from Sports where `Place` = '$place'")
);
if($date)(
$result4=mysqli_connect($con, "select * from Sports where `Date` = '$date'")
);
if($price)(
$result5=mysqli_connect($con, "select * from Sports where `Price` = '$price'")
);
}
?>
答案 0 :(得分:0)
使用if/elseif/
只执行一个查询,并将结果分配给同一个变量:
if ($type) {
$query = "select * from Sports where `Event Type` = '$type'";
} elseif ($team1) {
$query = "select * from Sports where `Team 1` = '$team1'";
} ...
} else {
die("You must fill in one of the search fields");
}
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($result)) {
// Code to display each row of results
}
答案 1 :(得分:0)
如果只想执行一个语句if/elseif/
if($type){
$result=mysqli_connect($con, "select * from Sports where `Event Type` = '$type'")
}
elseif($team1){
$result=mysqli_connect($con, "select * from Sports where `Team 1` = '$team1'")
}
elseif($team2){
$result=mysqli_connect($con, "select * from Sports where `Team 2` = '$team2'")
}