【发布时间】:2011-12-27 05:56:31
【问题描述】:
我在 php 代码中动态选择标签时遇到问题。我正在使用 php 创建一个网站(作业)并连接到一个包含所有所需信息的 oracle 数据库。每个agency 都有一个vehicules 列表。
我为agencies 和vehicules 创建了一个select option 标签。我希望车辆的数据根据用户选择的机构而改变。因此,如果用户选择agency A,则select option 标签上的车辆应该是agency A1 的车辆,而不是其他机构。到目前为止,我在select 标签中拥有所有机构的所有车辆。
我尝试创建一个函数来检查用户的输入并从用户选择的代理机构中选择车辆。然后它在函数中初始化一个$query。然后使用该查询填写我的select 标签。这是代码
function chooseVehicule(){
if( ($_POST['agence'])=='CARPORT'){
$query='SELECT marque, modele, nom_agence, num_immatriculation from vehicules v, agences ag
WHERE v.id_agence=ag.id_agence
AND ag.nom_agence=="CARPORT"';
}
}
然后
chooseVehicule();
但它给了我Undefined index: agence 错误。
请问有什么想法吗?这是我的完整代码。
<?php
function chooseVehicule(){
if( ($_POST['agence'])=='CARPORT'){
$query='SELECT v.marque, modele, ag.nom_agence, v.num_immatriculation from vehicules v, agences ag
WHERE v.id_agence=ag.id_agence
AND ag.nom_agence=="CARPORT"';
}
}
echo '<h3>'; echo 'Pour la location d\'une vehicule, veuillez remplir ce formulaire';echo'</h3>';
/*CLIENTS*/
$query="SELECT nom_client from CLIENTS";
$state=oci_parse($conn, $query);
oci_execute($state, OCI_COMMIT_ON_SUCCESS);
echo '
<form action="location.php" method="post">
<p><label for="client">Select your Name</label>
<select name="client" id="client">';
while(($row = oci_fetch_array($state, OCI_BOTH))){
echo'<option value="'.$row[0].'">'.$row[0]; echo'</option>';
}
echo'</select></p>';
echo '</form>';
/*AGENCES*/
$query="SELECT nom_agence from AGENCES";
$state=oci_parse($conn, $query);
oci_execute($state, OCI_COMMIT_ON_SUCCESS);
echo '
<form action="location.php" method="post">
<p><label for="agence">Select a Company</label>
<select name="agence" id="agence">';
while(($row = oci_fetch_array($state, OCI_BOTH))){
echo '<option value="'.$row[0].'">'.$row[0]; echo'</option>';
}
echo '</select></p>';
echo '</form>';
/*VEHICULES*/
$query="SELECT marque, modele, nom_agence, num_immatriculation from vehicules v, agences ag
WHERE v.id_agence=ag.id_agence
AND type_vehicule='UTILITAIRE'
order by nom_agence";
$state=oci_parse($conn, $query);
oci_execute($state, OCI_COMMIT_ON_SUCCESS);
echo '
<form action="location.php" method="post">
<p><label for="vehicule">Select a Vehicule</label>
<select name="vehicule" id="vehicule">';
echo '<optgroup label="Utilitaire">';
while(($row = oci_fetch_array($state, OCI_BOTH))){
echo '<option value="'.$row[3].'">'.$row[2]. ' ' . $row[0] . ' ' . $row[1]; echo '</option>';
}
echo '</optgroup>';
$query="SELECT v.marque, v.modele, ag.nom_agence, v.num_immatriculation from vehicules v, agences ag
WHERE v.id_agence=ag.id_agence
AND type_vehicule='VOITURE'
order by nom_agence";
echo '<optgroup label="Voiture">';
$state=oci_parse($conn, $query);
oci_execute($state, OCI_COMMIT_ON_SUCCESS);
while(($row = oci_fetch_array($state, OCI_BOTH))){
echo '<option value="'.$row[3].'">'.$row[2]. ' ' . $row[0] . ' ' . $row[1]; echo '</option>';
}
echo '</optgroup>';
echo'</select></p>';
echo '</form>';
oci_free_statement($state);
oci_close($conn);
?>
我没有使用函数chooseVehicule,因为它给了我一个错误。
谢谢。
【问题讨论】: