0

现在我有两个下拉列表,一个是名称,另一个是时间。我希望当用户在第一个下拉框中单击所需的名称时,第二个下拉框中显示的时间应该属于第一个下拉框中选择的名称。

$con=mysqli_connect("localhost","root","123","fyp");
$query1 = mysqli_query($con,"SELECT fname FROM lecturer");

echo "Select lecturer:<select name= 'fname'>";
$name = 'fname';
$name = mysql_real_escape_string($name);
while($row=mysqli_fetch_array($query1))
{
   echo "<option value='". $row['fname']."'>".$row['fname']. '</option>';
}
   echo '</select>';

$con=mysqli_connect("localhost","root","123","fyp");
$query3 = mysqli_query($con,"SELECT stime FROM studbooking WHERE lecname is '$name'");

echo "Select Booking time:<select name= 'stime'>";
while($row=mysqli_fetch_array($query3))
{
   echo "<option value='". $row['stime']."'>".$row['stime']. '</option>';
}
   echo '</select>';

如何在sql查询中使用php变量?

4

2 回答 2

1

您可以为此使用 mysqli->bind_param。

mysqli::bind_param

例子

$statement = $mysqli->prepare("SELECT stime FROM studbooking WHERE lecname=?");
$statement->bind_param('s', $name);
$statement->execute();

$result = $statement->get_result();
while ($row = $result->fetch_assoc()) {
    print_r($row);
} 
于 2013-10-26T10:32:09.423 回答
0

改变:

$name = mysql_real_escape_string($name);

至:

$name = mysqli_real_escape_string($con, $name);

您不能将mysql函数与mysqli连接一起使用,反之亦然。

于 2013-10-26T11:00:42.997 回答