php中的html选择选项标签(oracle数据库)

时间:2011-11-11 18:19:12

标签: php html oracle

我在php代码动态制作select标签时遇到问题。我正在使用php创建一个网站(家庭作业)并连接到具有所需信息的oracle数据库。每个agency都有一个vehicules列表。

我为select optionagencies创建了vehicules代码。我希望车辆的数据根据​​用户选择的机构而变化。因此,如果用户选择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,因为它给了我一个错误。 感谢。

2 个答案:

答案 0 :(得分:1)

$_POST超全局中的密钥将以与表单字段相同的名称发送到服务器。如果此表单正在提交给自己,则需要发送名为agence的字段以避免此错误。 PHP正在抛出错误,因为agence数组中没有键$_POST

为了绕过此检查,由于此页面正在处理多个表单,因此您可以先对每个键进行设置,然后再对其进行检查:

if( isset($_POST['agence']) && $_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"';
                    }

您应该对所有有选择地发送的表单执行此操作。

编辑:

你需要在页面中加入一些状态,'state'是指用户进入选择过程的程度。让我们这样做,以便车辆表单只显示用户是否已经选择了代理商。此表单应包含在条件中。

在此条件中包装车辆表格的所有逻辑:

if(isset($_POST['agence'])){
  $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>';
}

由于这个页面是程序性的,所以这样做是为了在为代理商做出选择之后,然后才会出现车辆表格。

我们需要让车辆的查询具有反映用户所做选择的WHERE子句。看起来您正在加入两个表中的id,因此where子句需要按代理ID或代理名称查找,具体取决于数据库设计。

如果将车辆表格放入这样的条件中,我们可以确定用户已经选择了代理商并且进展到表格的第二个“状态”。然后,您需要参数化您的sql语句并传入选定的ID。

答案 1 :(得分:0)

出于某种原因,这不是设置$ _POST ['agence']。 如果你做了一个isset($ _ POST ['agence']),它将返回false。

相关问题