显示下拉选择的值基于数据库中的两个表

时间:2015-03-21 15:52:46

标签: php mysql

我有两个表tb_tm(tm_id,tm_cat_id,tm_desc)和表tb_tm_cat(tm_cat_id,tm_cat)。如何在下拉列表中创建它,并根据以前创建的值以编辑形式选择。但是,我无法从数据库中获取值,下拉列表为空。以下是我的代码,请咨询。

Html表单

<form action="" method="post" name="form_etm">
        <table class="tb_etm">
            <tr> 
              <td>Category* : </td>
              <td>
                  <select name="tm_cat_id" required>
                    <option value="">Select Category</option>
                    <!--Fetch data from tb_category-->
                    <?php
                        while($getcat=mysql_fetch_array($result)) {
                    ?>
                        <!--Fetch tm_cat from tb_tm_cat based on tm_cat_id-->
                        <option value="<?php echo $tm_cat_id;?>"><?php echo $tm_cat;?></option>
                    <?php
                        }
                    ?>
                  </select>
              </td>
            </tr>
            <tr> 
              <td>Name* : </td>
              <td><input type="text" name="tm_desc" size="100" required value="<?php echo $tm_desc;?>"></td>
            </tr>
            <tr> 
              <td><input type="hidden" name="tm_id" value=<?php echo $tm_id;?>></td>
              <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>

编辑php

<?php
    $tm_id = $_GET['tm_id'];

    $result=mysql_query("SELECT tb_tm.*, tb_tm_cat.* FROM tb_tm, tb_tm_cat WHERE tb_tm.tm_cat_id = tb_tm_cat.tm_cat_id");

    while($row=mysql_fetch_array($result))
    {
        $tm_cat_id=$row['tm_cat_id'];
        $tm_desc=$row['tm_desc'];
    }
?>

更新php

<?php
    include_once("config_db.php");

    if(isset($_POST['update']))
    {
        $tm_id=$_POST['tm_id'];
        $tm_cat_id=$_POST['tm_cat_id'];
        $tm_desc=$_POST['tm_desc'];

        $result=mysql_query("UPDATE tb_tm SET tm_cat_id='$tm_cat_id',tm_desc='$tm_desc' WHERE tm_id='$tm_id'");

        header("Location: main_tm.php");
    }
?>

2 个答案:

答案 0 :(得分:0)

解决与显示(而不是更新)完全相关的问题,模式非常简单。

<?php

// adding this so you can catch failures
mysql_connect("host", "user","pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$query = "SELECT tb_tm.*, tb_tm_cat.* FROM tb_tm, tb_tm_cat WHERE tb_tm.tm_cat_id = tb_tm_cat.tm_cat_id";

// catch failures here too
$result = mysql_query($query) or die(mysql_error()."[".$query."]");

然后在你看来

<select name="tm_cat_id" required>
<?php 
while ($row = mysql_fetch_array($result))
    echo "<option value='".$row['path']."'>'".$row['name']."'</option>";

?>        
</select>

如果有机会,转向PDO。如果这是一个新生项目,那将是一个很好的长期举措。 我没有在年龄中使用过mysql_query

另外,我总是花一点时间来推荐拥抱MVC。例如,Twig和PHP设置非常简单,并提供更好的开发体验。

我也不能为ZF2喝饱;它实际上为你做了所有这些事情;)

祝你好运。

答案 1 :(得分:0)

我喜欢英语,但我说不好。很抱歉,如果我错了

从数据库获取信息

<?php

// Current tm_id
$tm_id = $_GET['tm_id'];

// Get tm by id
$result = mysql_query("SELECT * FROM tb_tm WHERE tm_id = '{$tm_id}'");
if (mysql_num_rows($result) < 1){
    die ('The TM Not Found!');
}
$tm = mysql_fetch_array($result);

// Get All tb_tm_cat 
$result_tb_tm_cat = mysql_query("SELECT * FROM tb_tm_cat");
?>

显示HTML信息:

<form action="" method="post" name="form_etm">
    <table class="tb_etm">
        <tr> 
          <td>Category* : </td>
          <td>
              <select name="tm_cat_id" required>
                <option value="">Select Category</option>
                <!--Fetch data from tb_category-->
                <?php while($getcat = mysql_fetch_array($result_tb_tm_cat)) { ?>
                    <!--Fetch tm_cat from tb_tm_cat based on tm_cat_id-->
                    <option value="<?php echo $getcat['tm_cat_id'];?>" <?php echo ($tm['tm_cat_id'] == $getcat['tm_cat_id']) ? 'selected' : ''; ?>><?php echo $getcat['tm_cat'];?></option>
                <?php } ?>
              </select>
          </td>
        </tr>
        <tr> 
          <td>Name* : </td>
          <td><input type="text" name="tm_desc" size="100" required value="<?php echo $tm['tm_desc'];?>"></td>
        </tr>
        <tr> 
          <td><input type="hidden" name="tm_id" value=<?php echo $tm['tm_id'];?>></td>
          <td><input type="submit" name="update" value="Update"></td>
        </tr>
    </table>
</form>

更新操作无关。