下面我有一个模块下拉菜单,它从db获取模块列表,显然在下拉菜单中显示它们:
$active = 1;
$sql = "SELECT ModuleId, ModuleNo, ModuleName FROM Module WHERE ModuleActive = ? ORDER BY ModuleNo";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->bind_param("i",$active);
$sqlstmt->execute();
$sqlstmt->bind_result($dbModuleId, $dbModuleNo, $dbModuleName);
$modules = array(); // easier if you don't use generic names for data
$output = "";
$moduleHTML = "";
$moduleHTML .= '<select name="module" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
while($sqlstmt->fetch())
{
$moduleno = $dbModuleNo;
$module = $dbModuleId;
$modulename = $dbModuleName;
$moduleHTML .= "<option value='".$module."'>" . $moduleno . " - " . $modulename . "</option>".PHP_EOL;
if (isset($_POST['module']) && ($_POST['module'] == $module)) {
$output .= "<p><strong>Selected Module:</strong> " . $moduleno . " - " . $modulename . "</p>";
}
}
$moduleHTML .= '</select>';
但我遇到的问题是,当我尝试将所选模块插入数据库时,它无法执行此操作,因此ModuleId为null。为什么不能将所选模块插入数据库?
if (isset($_POST['module'])) {
$_SESSION['module'] = $_POST['module'];
}
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$insertsql = "
INSERT INTO Session
(ModuleId)
VALUES
(?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("i", $_SESSION['module']);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
}
ModuleId是一个INT字段
答案 0 :(得分:0)
试试这个
if ($insert = $mysqli->prepare($insertsql)) {
$insert->bind_param("i", real_escape_string($_SESSION['module']));
$insert->execute();
$insert->close();
}
else {
// Handle query error here
}