2

下面我有一个模块下拉菜单,它从数据库中获取模块列表,并且显然将它们显示在下拉菜单中。它还在$output变量中显示所选模块编号和模块名称,并在隐藏输入 ( $hiddenoutput) 中显示所选模块的模块 ID(当文本输入在更改为隐藏之前为文本时,这已成功发送文本)

$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 = ""; 
 $hiddenoutput = "";
 $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>";
    $hiddenoutput .= "<p><input type='hidden' id='hiddenoutput' name='hiddenmodule' value='". $module ."'></p>";
}
  } 

  $moduleHTML .= '</select>'; 

  $submittedModuleId = (isset($_POST['module'])) ? $_POST['module'] : '';
  $output = (isset($output)) ? $output : '';
  $hiddenoutput = (isset($hiddenoutput)) ? $hiddenoutput : '';

详细信息显示在以下表格中:

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Module: <?php echo $moduleHTML; ?><input id="moduleSubmit" type="submit" value="Submit" name="submit" /></th>
</tr>
<tr>
<td><span id="errormsg"><?php echo $errormsg; ?></span><?php echo $hiddenoutput . $output; ?></td>
</tr>
</table>
</form>

但我真正不明白的是,在我下面的 INSERT sql 中,它能够检索和插入除一个之外的所有值,即隐藏输入中显示的 Module Id 的值。它一直在说:

Warning: mysqli_stmt::execute(): (23000/1048): Column 'ModuleId' cannot be null in ... on line 172

我的问题是为什么它不能检索并在数据库中插入模块 ID 的值?下面是代码:

session_start();

if (isset($_POST['hiddenmodule'])) {

$_SESSION['hiddenmodule'] = $_POST['hiddenmodule'];

}

if (isset($_POST['id'])) {

$_SESSION['id'] = $_POST['id'];

}

if(isset($_POST['sessionNum'])){
            //Declare my counter for the first time

            $_SESSION['initial_count'] = $_POST['sessionNum'];
            $_SESSION['sessionNum'] = $_POST['sessionNum'];

    }

if (!isset($_SESSION['sessionCount'])) {
    $_SESSION['sessionCount'] = 1;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    ++$_SESSION['sessionCount'];
}


$sessionMinus =  $_SESSION['sessionCount'];

if (isset($_POST['durationChosen'])) {

$_SESSION['durationChosen'] = $_POST['durationChosen'];

}

if (isset($_POST['dateChosen'])) {

$_SESSION['dateChosen'] = $_POST['dateChosen'];

}

if (isset($_POST['timeChosen'])) {

$_SESSION['timeChosen'] = $_POST['timeChosen'];

}

if (isset($_POST['textQuestion'])) {

$_SESSION['textQuestion'] = $_POST['textQuestion'];

}

if (isset($_POST['textMarks'])) {

$_SESSION['textMarks'] = $_POST['textMarks'];

}

if (isset($_POST['totalWeight'])) {

$_SESSION['totalWeight'] = $_POST['totalWeight'];

}

if (isset($_POST['buildings'])) {

$_SESSION['buildings'] = $_POST['buildings'];

}

if (isset($_POST['rooms'])) {

$_SESSION['rooms'] = $_POST['rooms'];

}

if (isset($_POST['teacherforename'])) {

$_SESSION['teacherforename'] = $_POST['teacherforename'];

}

if (isset($_POST['teachersurname'])) {

$_SESSION['teachersurname'] = $_POST['teachersurname'];

}

if (isset($_POST['teacherusername'])) {

$_SESSION['teacherusername'] = $_POST['teacherusername'];

}

var_dump($_POST);
var_dump($_SESSION);

 // connect to the database
 include('connect.php');

  /* check connection */
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
  }

$sql = "
  SELECT TeacherId
  FROM Teacher
  WHERE TeacherUsername = ?
";

if (!$stmt = $mysqli->prepare($sql)) {
  // Handle errors with prepare operation here
}

// Bind parameter for statement
$stmt->bind_param("s", $_SESSION['teacherusername']);

// Execute the statement
$stmt->execute();

// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$stmt->bind_result($teacherid);

// This populates $teacherid
$stmt->fetch();

// Close the statment
$stmt->close();

if($_SERVER['REQUEST_METHOD'] == 'POST')

{

    $time = str_replace(array(' Hrs ', ' Mins ', ' Secs'), array(':', ':', ''), $_SESSION['durationChosen']);

    for ($i = 1, $n = $_SESSION['sessionNum']; $i <= $n; $i++) {

            $insertsql = "
      INSERT INTO Session
        (SessionName, SessionTime, SessionDate, SessionWeight, SessionDuration, TotalMarks, ModuleId, TeacherId, Room)
      VALUES
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
    ";
    if (!$insert = $mysqli->prepare($insertsql)) {
      // Handle errors with prepare operation here
    }

        $sessname = $_SESSION['id'] . ($n == 1 ? '' : $i);
        $sessdate = date("Y-m-d", strtotime($_SESSION['dateChosen']));

        $insert->bind_param("sssisiiis", $sessname, $_SESSION['timeChosen'], $sessdate,
                     $_SESSION['totalWeight'], $time, $_SESSION['textMarks'],
                     $_SESSION['hiddenmodule'], $teacherid, $_SESSION['rooms']);

        $insert->execute();

        if ($insert->errno) {
          // Handle query error here
        }

        $insert->close();

    }


}

以下是var_dump($_SESSION)输出:

array(17) { 
["teacherid"]=> int(49) 
["teacherusername"]=> string(8) "u0867587" 
["teacherforename"]=> string(5) "Bruce" 
["teachersurname"]=> string(5) "Finch" 
["id"]=> string(5) "XNTLR" 
["initial_count"]=> string(1) "1" 
["sessionNum"]=> string(1) "1" 
["sessionCount"]=> int(1) 
["durationChosen"]=> string(22) "02 Hrs 00 Mins 00 Secs" 
["dateChosen"]=> string(10) "15-12-2012" 
["timeChosen"]=> string(5) "10:00" 
["textQuestion"]=> string(1) "1" 
["textMarks"]=> string(1) "5" 
["totalWeight"]=> string(1) "0" 
["buildings"]=> string(14) "Canalside East" 
["rooms"]=> string(7) "CE01/04" 
["hiddenmodule"]=> NULL }
4

1 回答 1

0

因为 hiddenmodule 没有发布。查看您的 var 转储。使用 firebug 检查表单以查看是否添加了此输入字段

于 2012-12-14T03:37:39.780 回答