【发布时间】: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