1

我正在使用dreamweaver,并且在一个页面上有两个表单,每个表单都使用php提交到同一个mySQL数据库,但不同的行。它们每个都有“粘性”代码,以防止它们在我将数据提交到mySQL时丢失值。问题是当我提交 form2 时 form1 会丢失值,反之亦然。整整两天,让我发疯。提前致谢。

<?php require_once('Connections/pmpConn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}



$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
  $updateSQL = sprintf("UPDATE readiness_one_day SET on_time=%s WHERE ready_primary=%s",
                       GetSQLValueString(isset($_POST['on_time2']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString($_POST['ready_primary2'], "int"));

  mysql_select_db($database_pmpConn, $pmpConn);
  $Result1 = mysql_query($updateSQL, $pmpConn) or die(mysql_error());

}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE readiness_one_day SET on_time=%s WHERE ready_primary=%s",
                       GetSQLValueString(isset($_POST['on_time1']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString($_POST['ready_primary1'], "int"));

  mysql_select_db($database_pmpConn, $pmpConn);
  $Result1 = mysql_query($updateSQL, $pmpConn) or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>

<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <p>
    <input name="ready_primary1" type="text" id="ready_primary1" value="1" size="2" maxlength="2" readonly="readonly" />
  </p>
  <p>
    <input name="on_time1" type="checkbox" id="on_time1" value="1"<?php if (isset($_POST['on_time1']) && ($_POST['on_time1'] == '1')) echo 'checked="checked" '; ?> /> <!code for sticky checkboxes>
    <label for="on_time1">On Time</label>
  </p>
  <p>
    <input type="submit" name="submit1" id="submit1" value="Update" />
  </p>
  <input type="hidden" name="MM_update" value="form1" />
</form>
<form id="form2" name="form2" method="POST" action="<?php echo $editFormAction; ?>" >
  <p>
    <input name="ready_primary2" type="text" id="ready_primary2" value="2" size="2" maxlength="2" readonly="readonly" />
  </p>
  <p>
    <input name="on_time2" type="checkbox" id="on_time2" value="1" <?php if (isset($_POST['on_time2']) && ($_POST['on_time2'] == '1')) echo 'checked="checked" '; ?> /> <!code for sticky checkboxes>
    <label for="on_time2">On Time</label>
  </p>
      <input type="submit" name="submit2" id="submit2" value="Update" />
  <input type="hidden" name="MM_update" value="form2" />
</form>
</body>
</html>
4

1 回答 1

1

您应该$_SESSION在提交每个表单后立即设置变量,以便您可以在页面之间保留值。之后,您可以执行以下操作:

在表单提交页面:

<?php 
   session_start(); 
   $SESSION['one_box'] = True;
?>

在带有表格的页面上:

<?php 
start_session(); 
function selected($value) {
   if (isset($_SESSION[$value]) {
      return "checked='yes'";
   }
   else {
      return "";
   }
?>

<input type="checkbox" name="one_box" <?php echo selected("one_box"); ?> />
于 2012-07-29T02:20:04.713 回答