0

我正在尝试填充一个从MySQL检索数据库的下拉框。下面是我的表单php文件。对我来说它看起来不错,但我不明白为什么当我点击提交按钮时它没有显示任何东西。我能做些什么来解决这个错误吗?任何更正和建议将不胜感激。

CS_JOBS表包括:job_title, category_code(17, 27, 37....), ...等。

connect.php在这种情况下工作正常。

我的表格

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <form action = "csjob.php" method = "POST" name = "jobsearch">
            <select name = "category_code[]">
                <option value = "17">Architecture and Engineering</option>
                <option value = "27">Arts, Design, Entertainment, Sports, and Media</option>
                <option value = "37">Building and Grounds Cleaning and Maintenance</option>
                <option value = "13">Business and Financial Operations</option>
                <option value = "21">Community and Social Services</option>
                <option value = "15">Computer and Mathematical</option>
                <option value = "47">Construction and Extraction</option>
                <option value = "25">Education, Training, and Library</option>
                <option value = "45">Farming, Fishing, and Forestry</option>
                <option value = "35">Food Preparation and Serving Related</option>
                <option value = "29">Healthcare Practitioner and Technical</option>
                <option value = "31">Healthcare support</option>
                <option value = "49">Installation, Maintenance, and Repair</option>
                <option value = "23">Legal</option>
                <option value = "19">Life, Physical, and Social Science</option>
                <option value = "11">Management</option>
                <option value = "43">Office and Administrative Support</option>
                <option value = "39">Personal Care and Service</option>
                <option value = "51">Production</option>
                <option value = "33">Protective Service</option>
                <option value = "41">Sales and Related</option>
                <option value = "53">Transportation and Material Moving</option>
            </select>
            <input type="submit" value="Submit"> 
        </form>
    </body>
</html>

csjob.php

<?php 
    require("connect.php");
    if(isset($_POST['submit'])){
        $sql = "SELECT * FROM CS_JOBS WHERE category_code=".$_POST['category_code'];
        $result = mysql_query($sql);

        echo "<select name='category_code'>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='".$row['job_title']"'>".$row['job_title']"</option>";
        }
        echo "</select>";
    }
?>

这个问题有很多错别字,这是我上传的实际代码:链接

4

3 回答 3

2

你有3个小问题。第一个是形式

<select name = "category_code[]">

应该

<select name="category_code">

因此,如果[]名称中没有括号,也不需要额外的空格......
只有当您有一个具有多项选择的选项时才需要括号,并且将返回一个数组(因此是括号)。在你的情况下没有发生这种情况。

第二个问题是之前所说的:在 PHP 中

$row['job_title'}

应该

$row['job_title']

所以}改变]

您的第三个问题与检查提交有关:

if(isset($_POST['submit'])){

应该

if(isset($_POST['category_code'])){

因为您没有名为“提交”的字段。您有一个带有id submit 和value submit 的字段。但没有名字提交。您可以添加name='submit'到提交按钮。或者只是按照我上面提出的更改...

于 2013-07-31T17:46:45.257 回答
1
<?php 
    require("connect.php");
    if(isset($_POST['submit']))
    {
        $sql = "SELECT * FROM CS_JOBS WHERE category_code=".$_POST['category_code'];
        $result = mysql_query($sql);
        if( mysql_num_rows( $result ) > 0 ){
            echo "<select name='category_code'>";
              while ($row = mysql_fetch_array($result)) {
                  echo "<option value='".$row['job_title']"'>".$row['job_title']"</option>";
                }

            echo "</select>";
          }
    }


?>
于 2013-07-31T17:39:26.400 回答
0

你的代码没有意义。如果存在特定的表单字段,则运行查询,但仅在存在相同的表单字段时运行“显示结果”代码。

最简单的解决方法:删除} else {...

于 2013-07-31T17:35:57.287 回答