我正在编写一个 PHP 页面,该页面有一个下拉状态选择和一个用于评论的文本区域。目前,我无法弄清楚如何编码,以便下拉列表和文本区域在提交时正确更新。
我已将下拉菜单的默认设置为显示“---选择状态---”,没有任何值。
我的问题是:每当我选择状态并在评论区域进行更改时,只有状态会更新,评论保持不变。我想知道是否有更有效的方法来做到这一点..
更新:我已经考虑过了......有4种情况:
- 状态改变/评论改变
- 状态不变/评论改变
- 状态已更改/评论未更改
- 状态不变/评论不变
我该如何进行编码以使其在所有情况下都更新?
这是我的下拉状态/评论文本区域的代码:
// this is the function for status dropdown menu
function statusDropdown($case){
print("<b>Status:</b>");
$dropdown = "<select name = 'status'><option selected='selected' value=NULL>--Select Status--</option>";
$connection = getMySqlConnection();
$sql = "SELECT STATUS_ID, STATUS_NAME FROM primary_status_lookup ORDER BY STATUS_ID ASC";
$result = mysql_query($sql, $connection) or die(mysql_error());
while($record=mysql_fetch_array($result)){
$dropdown .= "<option value = '{$record['STATUS_ID']}'> {$record['STATUS_NAME']}</option>";
}
$dropdown .="</select>";
echo $dropdown;
}
//This part incorporates Status dropdown & Comments (text area)
function tableStatus($case) {
$connection = getMySqlConnection();
$sql = "SELECT statistics_status, statistics_comments FROM cases WHERE caseid='".$case."'";
$result = mysql_query($sql, $connection) or die(mysql_error());
if($result!== FALSE){
while ($record = mysql_fetch_row($result)) {
$status=$record[0];
$comments=$record[1];
print("<form><p>");
showStatusComment($case);
statusDropdown($case);
print("<input type=hidden name='case' value='".$case."'/>");
print(" <label><b>Comments:</b><textarea name='comments' cols=70 rows=2 >".$comments."</textarea></label><br/><br/>");
print("<input type='submit' name='submit' value='Submit'/></form>");
}
}
}
这是我更新数据的代码:
function saveTableStatus($case)
{
//retrieve selected status
if(isset($_REQUEST['status'])) {
$status = $_REQUEST['status'];
}
//retrieve typed comments
if(isset($_REQUEST['comments'])) {
$comments = $_REQUEST['comments'];
}
if($status=='NULL') {
print("<p class='error'>No status selected, please select a status and try again.</p>");
}
else if (($status!=='NULL')){
$connection = getMySqlConnection();
mysql_query("START TRANSACTION", $connection);
$result= mysql_query("Update cases Set statistics_status=".$status.", statistics_comments =".mysql_real_escape_string($comments)." Where caseid='".mysql_real_escape_string($case)."'", $connection);
if($result) {
mysql_query("COMMIT", $connection);
print("<p class='saved'>Table Status Updated!</p>");
} else {
mysql_query("ROLLBACK", $connection);
}
mysql_close($connection);
}
}