下拉列表中的表单值未通过

时间:2014-01-24 05:10:41

标签: php mysql forms

我创建了一个由数据库填充的下拉列表,现在我无法检索数据。通常情况下,如果我必须手动命名数据,我会知道如何检索下拉列表的值,但在这种情况下,我不太确定如何命名它。

这是我目前的代码:

   <h1>Generate Reports</h1>

     <form enctype="multipart/form-data" action="http://localhost/yiiFolder/index.php/create" method="post">
       <table>
             <tr>
                <td><strong>Materials</strong></td>
<?php
   mysql_connect('host', 'root', 'password');
   mysql_select_db ("db");
   $sql = "SELECT material_name FROM materials";
   $result = mysql_query($sql);

   echo "<td><select name='materials'>";

       while ($row = mysql_fetch_array($result)) 
        {
            echo "<option value='" . $row['material_name'] . "'>" . 
            $row['material_name'] . "</option>";
        }

       echo "</select></td></tr> ";

       $sql2 = "SELECT location_name From locations";
       $result2 = mysql_query($sql2);

?>


 <td><strong>Locations</strong></td>
   <?php
 echo "<td><select name='locations'>";
    while ($row2 = mysql_fetch_array($result2)) 
        {
            echo "<option value='" . $row2['location_name'] . "'>" . 
            $row2['location_name'] . "</option>";
        }   

echo "</select></td></tr>";

   ?>
                    <tr>
                        <td><button name="submit" type=submit>Generate</button></td>
                    </tr>
  </table>

 </form>


   <?php 

$material = $row['material_name'];
$locations = $row2['location_name'];
$generate = $_POST['submit'];

  if(isset($generate))
{
    echo $material;
    echo $locations;
}

 ?>

1 个答案:

答案 0 :(得分:0)

您尝试在点击提交按钮之前捕获值。此外,正如Hanky指出你在引用选择数据时使用了错误的名字。你应该这样做

if(isset($_POST['submit'])) // this code will run after the button is clicked
{
    $material = $_POST['materials']; // and not material_name
    $locations = $_POST['locations']; // and not location_name
    echo $material;
    echo $locations;
}

PS:您正在采用非常不安全的方式开发Web应用程序。至少你需要切换到PDO并始终逃避数据。