gpt4 book ai didi

php - 编辑表单中的单选按钮值不是 "checked"

转载 作者:行者123 更新时间:2023-11-29 13:59:12 25 4
gpt4 key购买 nike

我有一个表格,每行都有编辑选项。当我单击“编辑”时,会弹出一个表单,并显示填写了要编辑的数据的字段。但是,所选的单选按钮未显示。
你能告诉我的代码中缺少什么吗?比如如何让正确的单选按钮显示选中的值?

这是编辑表单:

<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']);
}

最佳答案

我从您的加入中假设相关字段是“genre_id”,而不是“genre”:

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

关于php - 编辑表单中的单选按钮值不是 "checked",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15350068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com