【问题标题】:How to save PHP form data to a session variable without updating database in Dreamweaver?如何将 PHP 表单数据保存到会话变量而不更新 Dreamweaver 中的数据库?
【发布时间】:2015-05-30 08:00:50
【问题描述】:

我正在使用 Dreamweaver 构建我的 PHP/MySQL 站点,因为我的技能相当低,我想将多选项下拉列表中的选择保存到 $_SESSION 变量中。在使用上一页上的以下内容更新数据库中的字段时,我已成功保存到会话变量,并且似乎正在工作(Dreamweaver 插入的页面顶部,我评论了我编辑它以使其存储到$_SESSION):

<?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']);
}
/// this is what to edit to make session variables from a form
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "SelectBookForm")) {
  $insertSQL = sprintf("INSERT INTO characters (character_name1, play_system, character_owner) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['NewCharacterNameInput'], "text"),
                       GetSQLValueString($_POST['select'], "text"),
                       GetSQLValueString($_POST['CharacterOwner'], "int"));
                       $_SESSION['play_system'] = GetSQLValueString($_POST['select'], "text");
                       $_SESSION['character_owner'] = GetSQLValueString($_POST['CharacterOwner'], "int");
                       $_SESSION['character_name1'] = GetSQLValueString($_POST['NewCharacterNameInput'], "text");

  mysql_select_db($database_DLP_RPG, $DLP_RPG);
  $Result1 = mysql_query($insertSQL, $DLP_RPG) or die(mysql_error());

  $insertGoTo = "character_new_book_select.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_DLP_RPG, $DLP_RPG);
$query_login = "SELECT * FROM users";
$login = mysql_query($query_login, $DLP_RPG) or die(mysql_error());
$row_login = mysql_fetch_assoc($login);
$totalRows_login = mysql_num_rows($login);

mysql_select_db($database_DLP_RPG, $DLP_RPG);
$query_play_systems_recordset = "SELECT * FROM play_systems";
$play_systems_recordset = mysql_query($query_play_systems_recordset, $DLP_RPG) or die(mysql_error());
$row_play_systems_recordset = mysql_fetch_assoc($play_systems_recordset);
$totalRows_play_systems_recordset = mysql_num_rows($play_systems_recordset);

mysql_select_db($database_DLP_RPG, $DLP_RPG);
$query_CharacterOwner = "SELECT * FROM users WHERE users.user_id = 
    (SELECT user_id FROM users WHERE user_login = '{$_SESSION['MM_Username']}')";
$CharacterOwner = mysql_query($query_CharacterOwner, $DLP_RPG) or die(mysql_error());
$row_CharacterOwner = mysql_fetch_assoc($CharacterOwner);
$totalRows_CharacterOwner = mysql_num_rows($CharacterOwner);
?>

这就是 Dreamweaver 用来在页面正文中制作表单的方法(同样,这是有效的):

<form action="<?php echo $editFormAction; ?>" name="SelectBookForm" method="POST" id="SelectBookForm"> 
  <select name="select" size="1" form="SelectBookForm">
    <?php
do {  
?>
    <option value="<?php echo $row_play_systems_recordset['play_system']?>"><?php echo $row_play_systems_recordset['play_system']?></option>
    <?php
} while ($row_play_systems_recordset = mysql_fetch_assoc($play_systems_recordset));
  $rows = mysql_num_rows($play_systems_recordset);
  if($rows > 0) {
      mysql_data_seek($play_systems_recordset, 0);
      $row_play_systems_recordset = mysql_fetch_assoc($play_systems_recordset);
  }
?>
  </select>
  <input name="CharacterOwner" type="hidden" id="CharacterOwner" value="<?php echo $row_CharacterOwner['user_id']; ?>"><input name="NewCharacterNameInput" type="text" required id="NewCharacterNameInput" form="SelectBookForm" placeholder="Give your character a name!" size="25" maxlength="128">
      <BR>
  <input name="NewCharacterSubmit" type="submit" id="NewCharacterSubmit" form="SelectBookForm" value="Select system and start my character">

      <input type="hidden" name="MM_insert" value="PlaySystemForm">
      <input type="hidden" name="MM_insert" value="SelectBookForm">
</form>

似乎没有选项只保存到$_SESSION,而不是输入或更新到 Dreamweaver 中的数据库。每次我尝试创建记录集时,我都会在第 114 行遇到错误,这只是两个 } 括号之间的空格。

这是我要创建的页面形式:

<form method="post" id="BookSelectionForm">
<select name="BookSelections" size="10" multiple id="BookSelections" form="BookSelectionForm">
  <?php
do {  
?>
  <option value="<?php echo $row_BooksRecordset['book']?>"><?php echo $row_BooksRecordset['book']?></option>
  <?php
} while ($row_BooksRecordset = mysql_fetch_assoc($BooksRecordset));
  $rows = mysql_num_rows($BooksRecordset);
  if($rows > 0) {
      mysql_data_seek($BooksRecordset, 0);
      $row_BooksRecordset = mysql_fetch_assoc($BooksRecordset);
  }
?>
</select>

<input name="BookSelectionFormSubmitButton" type="submit" id="BookSelectionFormSubmitButton" form="BookSelectionForm" formmethod="POST" value="Select these campaigns">
</form>

根据本文顶部示例中的输入,它可以正常工作并严格限制数据应有的方式。长话短说,我想将多选项下拉列表中的选择存储为$_SESSION['book'] 变量,以便我可以在下一页使用它。有人可以帮助我走上正确的道路来实现这一目标吗?

不确定它有多少价值,但这里也是 Dreamweaver 允许无错误的记录集信息的顶部。

<?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;
  }


}

mysql_select_db($database_DLP_RPG, $DLP_RPG);
$query_login = "SELECT * FROM users";
$login = mysql_query($query_login, $DLP_RPG) or die(mysql_error());
$row_login = mysql_fetch_assoc($login);
$totalRows_login = mysql_num_rows($login);

/// this one sets the play_system to the one selected at the beginning and can be used to restrict results by using the variable $BooksRecordset, I probably need to change this because it's confusing.  It's giving the play_system and not the books.  Maybe RealBooksRecordset is better for now.
mysql_select_db($database_DLP_RPG, $DLP_RPG);
$query_BooksRecordset = "SELECT * FROM character_system WHERE character_system.system_name = ({$_SESSION['play_system']})";
$BooksRecordset = mysql_query($query_BooksRecordset, $DLP_RPG) or die(mysql_error());
$row_BooksRecordset = mysql_fetch_assoc($BooksRecordset);
$totalRows_BooksRecordset = mysql_num_rows($BooksRecordset);



?>

编辑:我知道为什么我在第 114 行得到错误,因为它是一个错误的括号,但仍然无法让它添加记录集。

【问题讨论】:

  • 你有没有在页面顶部写session_start()??
  • 是的,会话正在运行,我尝试输入最小代码,因为我用完了空间并且之前的帖子中有太多字符。页面加载,下拉列表使用会话变量正确限制数据。我想发布到一个新的会话变量。
  • 使用框架,例如:laravel
  • 我认为我没有权限在我的主机上安装那种包,但我会调查一下。看看这个网站,这个框架的使用可能超出了我的技能范围。

标签: php mysql forms session post


【解决方案1】:

(我第一次看到这篇文章时,我以为这是个玩笑。然后我浏览了你的一些答案,我觉得你是 SO coderot 的受害者,你不能怪。用谷歌的方式来垃圾 PHP 太容易了。)

长话短说,我想将多选项下拉列表中的选择存储为 $_SESSION['book'] 变量,以便在下一页使用它。 有人可以帮助我走上正确的道路来实现这一目标吗?

当然。所以,首先:告诉一群程序员你使用 Dreamweaver 编写 PHP 是因为你缺乏技能,就像告诉一群猎人你正在用弹弓追捕一只熊,因为你不知道如何射枪:期待很多关于射枪或打兔子的建议,但不要期待弹弓有太多帮助。

tl;dr:Dreamweaver 无法帮助您。这是您为获得答案而苦苦挣扎的部分原因。

ಠ_ಠ

仅供参考,这是您无法让它添加记录集的最大原因,因为这种方法在十多年来一直不合理。至少use something actually supported

希望这能让您指明正确的方向。如果您需要更多帮助,我会在附近。

【讨论】:

  • 我知道我最终将不得不迁移到 mysqli 或其他连接数据库的方法之一,但页面上的内容是 Dreamweaver 在其“服务器行为”中加入的内容。此页面不会存储任何有价值的数据,它只是作为概念验证,以便我可以向潜在的开发人员展示并获得对工作的一些估计。我将研究序列化并尝试弄清楚如何将其应用于我需要的输出。否则,我会想出某种解决方法,将数据发布到表中,以便获得所需的 $_SESSION。谢谢!
  • @JoeCook 您确实意识到必须完全重构您的概念证明的期望至少会使我的时间估计再次增加一半,对吗?是的,我知道 Dreamweaver 域服务器端生成的代码是什么样的,而且我知道从一个强烈坚持我确实需要它的人那里得到一个 PoC 需要花费多少时间(提示:我不需要)什么时候很有可能他真的只是想看到他的代码部署到生产环境中并且需要我的帮助“让它工作”。 (你可以从他拒绝明智的建议,尤其是重构方面的努力来判断。;))
  • 我认为您可能缺少的部分是 serializeunserialize 配对:php.net/manual/en/function.unserialize.php。您可以调用 serialize 将值放入响应中的 $_SESSION 变量中,然后在下一个请求中使用 unserialize 将它们拉回代码中。
  • @JoeCook Misandrist 的观点是我确实回答了你的问题,你所要做的就是尝试。完全没有。唯一将您的问题与 Stack 上的免费工作请求(因此得到审核,仅供参考)区分开来的部分是您要求指向正确方向的部分。*您被指向正确的方向。
  • 感谢您的帮助,并且我按照建议对序列化进行了调查,没有讽刺意味。我不知道为什么它被删除或似乎煽动了这种愤怒。回答顶部的问题是它不允许我通过 DW 从按钮保存到变量。我仍在试图弄清楚当用户按下按钮时会发生什么,但它看起来与 $editFormAction 有关。我将尝试从操作中删除与服务器的连接并让它设置变量。我想我可以把 serialize 命令放在那里。
猜你喜欢
  • 2016-08-13
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 2019-01-22
相关资源
最近更新 更多