我想从下拉菜单中删除会话。用户将从下拉菜单中选择一个会话,然后单击删除按钮,然后应该删除数据库中的会话。问题是,如果我点击删除按钮,它会出现一个未定义的索引错误,表明{200}中未定义sessionsDrop
我的问题是:
1:如何摆脱下面代码中未定义的索引错误 2:如何以及在何处编写if语句,以便在删除成功时,它将回显"会话...被删除"否则,如果它没有删除,则显示echo"发生错误,会话...未被删除"
以下是代码:
$sessionnum = 0;
if (isset($_POST['modulesubmit'])) {
if($_REQUEST['modulesDrop']==''){
echo "Please Select a Module from the Drop Down Menu Above";
}
else{
var_dump($_POST['modulesDrop']);
$sessionquery = "
SELECT SessionId, SessionDate, SessionTime, ModuleId
FROM Session
WHERE
(ModuleId = ?)
ORDER BY SessionDate, SessionTime
";
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("s",$_POST['modulesDrop']);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
$dataArraySession = array();
while ( $sessionqrystmt->fetch() ) {
$dataArraySession[$dbSessionId]['SessionDate'] = $dbSessionDate;
$dataArraySession[$dbSessionId]['SessionTime'] = $dbSessionTime;
}
$sessionHTML = "";
$sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($dataArraySession as $sessionId => $sessionData) {
$sessionHTML .= "<option value='$sessionId'>" . $sessionId . " - " . $sessionData['SessionDate']. " - " . $sessionData['SessionTime'] ."</option>".PHP_EOL;
}
$sessionHTML .= '</select>';
}
if ($sessionnum > 0) {
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Sessions: <?php echo $sessionHTML; ?><input id="sessionSubmit" type="submit" value="Delete" name="sessionsubmit" /></p>
</form>
<?php
}
else {
echo "<p>Sorry, You have No Sessions under this Module</p>";
}
}
if (isset($_POST['sessionsubmit'])) {
if($_REQUEST['sessionsDrop']==''){ //line 200 where undefined index occurs
echo "Please Select a Session from the Drop Down Menu Above to Delete a Session";
}
else{
var_dump($_POST['sessionsDrop']);
$sessiondel = $_POST['sessionsDrop'];
$sessiondeletesql = " DELETE sess
FROM Session AS sess
WHERE sess.SessionId = ?";
if (!$delete = $mysqli->prepare($sessiondeletesql)) {
// Handle errors with prepare operation here
}
//Dont pass data directly to bind_param store it in a variable
$delete->bind_param("s", $sessiondel);
$delete->execute();
if ($delete->errno) {
// Handle query error here
}
$delete->close();
echo "Session $sessiondel was Deleted";
}
}
?>
答案 0 :(得分:0)
您的undefined index
错误是因为您使用的是$_REQUEST['sessionsDrop']==''
,但您的表单代码是 -
$sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;
所以你应该使用$_REQUEST['session']==''
而不是$_REQUEST['sessionsDrop']==''
,因为超级全局($_POST
,$_GET
,$_REQUEST
)引用了name
,而不是id
。