单选按钮值未以编辑形式“选中”

时间:2013-03-11 22:42:44

标签: php mysql forms radio-button

我有一个表格,每行都有选项编辑。当我单击编辑时,弹出一个表单并显示填写了要编辑的数据的字段。但是,所选单选按钮未显示 你能告诉我的代码中缺少什么吗?就像我怎样才能使正确的单选按钮显示检查值?

以下是编辑表单:

<form action="edit_movie.php" method="post">
<input type="hidden" name="id" value="<?php echo $movie['id']; ?>" /> <br>
Title:<br> <input type="text" name="title" value="<?php echo $movie['title']; ?>" /> <br>
Release Year:<br> <input type="text" name="release_year" value="<?php echo $movie['release_year']; ?>" /> <br>
Director:<br> <input type="text" name="director" value="<?php echo $movie['director']; ?>" /> <br><br>
Select genre:
<br> <input type="radio" name="genre_id" value="1"<?php if($movie['genre'] == 1) { echo ' checked'; } ?> />Action<br>
<br> <input type="radio" name="genre_id" value="2"<?php if($movie['genre'] == 2) { echo ' checked'; } ?> />Comedy<br>
<br> <input type="radio" name="genre_id" value="3"<?php if($movie['genre'] == 3) { echo ' checked'; } ?> />Drama<br>
<br> <input type="radio" name="genre_id" value="4"<?php if($movie['genre'] == 4) { echo ' checked'; } ?> />Horror<br>
<br> <input type="radio" name="genre_id" value="5"<?php if($movie['genre'] == 5) { echo ' checked'; } ?> />Romance<br>
<br> <input type="radio" name="genre_id" value="6"<?php if($movie['genre'] == 6) { echo ' checked'; } ?> />Thriller<br>
<input type="submit" value="Update movie" />
</form>

以下是编辑的代码:

function get_movie_by_id($id){

   $query = sprintf("SELECT * FROM movies m INNER JOIN categories c ON m.genre_id = c.genre_id WHERE id = '%s' LIMIT 1", mysql_real_escape_string($id));

   $result = mysql_query($query);

   if ( ! $result ){
       return false;
   } else {
         $return = array();

         while ($row = mysql_fetch_assoc($result)){

                $return = array('director' => $row['director'], 'genre' => $row['genre'], 'release_year' => $row['release_year'], 'title' => $row['title'], 'id' => $row['id']); 
         }
             return $return;
     }
}



if ( isset($_GET['edit']) ){

   $movie = get_movie_by_id($_GET['edit']);
}

1 个答案:

答案 0 :(得分:1)

我假设你的联盟中有关的字段是“genre_id”,而不是“genre”:

$return = array('director' => $row['director'], 'genre' => $row['genre_id'], 'release_year' => $row['release_year'], 'title' => $row['title'], 'id' => $row['id']);