我有一個表格,每行都有選項編輯。當我點擊編輯時,會彈出一個表單並顯示填寫了要編輯的數據的字段。但是所選的單選按鈕沒有顯示。
你能告訴我的代碼中缺少什麼嗎?像我如何使正確的單選按鈕顯示選中的值?單選按鈕值未在編輯表單中「檢查」
下面是編輯表單:
<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']);
}
什麼在'$ mo爭奪[ '風格']'? – kapa 2013-03-11 22:44:43
嗨!它與genre_id相同。實際上我有兩個表,一個是流派,他們在genre_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)); – Lisa 2013-03-11 22:47:07
我懷疑'genre'和'genre_id'雖然是不一樣的...檢查它。 – kapa 2013-03-11 22:48:38