如何使用 HTML 主体中的选择选项值作为 PHP SQL 语句中的变量 同一页面


how to use select option value in html main body as variable in php sql statement same page

正如我从上到下的顺序理解PHP过程。 因此,您必须在使用变量的语句之前定义一个变量。

但是,当下面的选择值更改也应该自动更改范围时,我将如何使在主体中定义为变量的选择选项值可用于上面的语句?

例。

    <?php
 // connect to MySQL
$conn1 = dbConnect('admin');
// prepare the SQL query
$sql = "SELECT * FROM contract WHERE cont_num = '$contract_number'";
// submit the query and capture the result
$micko = $conn1->query($sql) or die(mysqli_error());
?>

我的 HTML,变量contract_number被定义为下表中选择选项的变量。 如何将其传递回 SQL 语句的顶部。 我还希望当选择值随着变量值的变化而更改时自动重新加载表,以便返回正确的信息。 或者,如果您有更好的方法,我可以这样做,请提供帮助。

<table>
<?php
while ($ayanda = $micko->fetch_assoc()) {
?>
<tr>
<select name = "contract_no">
<!--these select options are derived from a different sql statement as the rest of the  table-->
<?php
while ($row = $chuchu->fetch_assoc()) {
?>
<option><?php echo $row['cont_num']; ?></option>
<?php  $contract_number = '<option value="'.$row.'">'.$value.'</option>';?> 
<?php } ?>
</select>
<td></td>
<td><strong>Contruct Customer:</strong></td>
<td><?php echo $ayanda['cont_custm']; ?></td>
</tr>

<tr>
<td><strong>Account Number:</strong></td>
<td><?php echo $ayanda['accnt_num']; ?></td>
<td></td>
<td><strong>Contruct Volume:</strong></td>
<td><?php echo $ayanda['cont_vol']; ?></td>
</tr>

<tr>
<td><strong>Created:</strong></td>
<td><?php echo $ayanda['created']; ?></td>
<td></td>
<td><strong>Broker:</strong></td>
<td><?php echo $ayanda['broker']; ?></td>
</tr>
<?php } ?>
</table>

因为 php 是一种基于服务器的脚本语言,所以您必须与 php 文件建立异步连接。例如,您可以使用 AJAX 执行此操作。

使用所选值作为 get 或 post 参数加载 php 文件。然后,您可以在 php 文件中输出 sql 语句的结果,并在 ajax 引擎中获取此值。