我有一个收音机盒和2个下拉菜单,当提交保存到mysql时。单选框是yes或no,以及在html中创建的2个下拉列表。它目前完全正常工作并保存所有数据。
我现在想要做的是当用户重新登录时,它将显示他们之前选择的内容(如果有)。
PHP SCRIPT:
<?php
session_start();
require_once("config.php");
if(!isset($_SESSION['username'])){
header('Location: login.php');
exit;
}else{
$sql = "SELECT attendance1 FROM user WHERE username = '".mysql_real_escape_string($_SESSION['username'])."'";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
if(($row[0] == "Yes") || ($row[0] == "No")){
header("Location: errorsubmit.html");
exit;
}
}
if(isset($_POST['submit'])){
$sql = "UPDATE user SET attendance1 = '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET colour1= '" . mysql_real_escape_string($_POST['colour1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET shade1= '" . mysql_real_escape_string($_POST['shade1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
header("Location: thanks.html", true, 303);
}
?>
FORM:
<form>
<input name="attendance1" type="radio" id="Yes" value="Yes" checked="checked"/>Yes
<br />
<input name="attendance1" type="radio" id="No" value="No" />No
</h3></td>
<td>
<select name="colour1" id="colour1" >
<option selected="selected">Please Select</option>
<option>Red</option>
<option>White</option>
<option>Green</option>
</select>
</td>
<td><h3>
<select name="shade1" id="shade1" >
<option selected="selected">Please Select</option>
<option>light</option>
<option>heavy</option>
</select>
<td> </td>
<td><label>
<input type="submit" name="submit" id="button" value="Submit" />
</label></td>
</tr>
</table>
答案 0 :(得分:2)
尝试使用以下内容:
您需要从数据库中获取值并将它们与选择框值匹配以显示它们已选中。
<select name="shade1" id="shade1" >
<option>Please Select</option>
<option value="light" <?php if($val=='light') echo 'selected'; ?>>light</option>
<option value="heavy"<?php if($val=='heavy') echo 'selected'; ?>>heavy</option>
</select>
这里$val
是变量,具有从数据库中检索的值。
虽然添加你应该:
<option value="light" >light</option>
<option value="heavy">heavy</option>
答案 1 :(得分:1)
您只需检查数据库中的值是否具有选项字段的值,如果是,则将“selected ='true'”回显到您的选项标记。像
<option <?php if($row["column_name"] == "light") echo "selected=\"true\""; ?>>light</option>