我想创建可以从数据库中检索数据的PHP页面,但是例如当用户为年龄放置15并且选择类别"男性"然后数据显示男性和女性。我怎样才能使它更具特异性,男性为男性,女性为女性男性和女性。男孩和男孩的孩子孩子等? `
<?php
// check if the form has been submitted and display the results
if(isset($_POST["Age"]) && isset($_POST["Categories"])){
define('DB_NAME', 'test');
define('DB_USER', 'test');
define('DB_PASSWORD', 'test');
define('DB_HOST', 'localhost');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn){die('Could not connect: ' . mysqli_connect_error());}
// escape the post data to prevent injection attacks
$Age = mysqli_real_escape_string($conn, $_POST['Age']);
$Categories = mysqli_real_escape_string($conn, $_POST['Categories']);
$sql = "SELECT * FROM `Medic Rate` WHERE `Age` LIKE '%$Age%' AND `Categories` LIKE '%$Categories%'";
$result=mysqli_query($conn, $sql);
// check if the query returned a result
if(!$result){echo 'There are no results for your search';}
else{
// result to output the table
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr>
<th>Medic_Id</th>
<th>Age</th>
<th>Categories</th>
<th>Plan</th>
<th>Rate</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr><td>";
echo $row['Medic_Id'];
echo "</td><td>";
echo $row['Age'];
echo "</td><td>";
echo $row['Categories'];
echo "</td><td>";
echo $row['Plan'];
echo "</td><td>";
echo $row['Rate'];
echo "</td></tr>";
}
echo "</table>";
}
mysqli_close($conn);
} // end submitted
else{// not submitted to output the form
?>
<form action=" " method="post">
<label>Enter Age:</label>
<input name="Age" type="number" placeholder="Type Here"><br><br>
<label>Enter Categories:</label>
<select name="Categories" />
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Male & Child">Male & Child</option>
<option value="Female & Child">Female & Child</option>
<option value="Insured & Spouse">Insured & Spouse</option>
<option value="Insured & Family">Insured & Family</option>
</select><br><br>
<input type="submit" value="Enter">
</form>
<?php } // end not submitted?>`
答案 0 :(得分:0)
尝试将<select name="Categories" />
更改为<select name="Categories">
删除反斜杠并删除脚本末尾的后退标记。