I want to delete a session from a drop down menu. The user will select a session from the drop down menu and then click on the delete button, this should then delete session in the database. The problem is that if I click on the delete button, it then comes up with an undefined index errors stating sessionsDrop are undefined in line 200 My question is:
1: How can I get rid of the undefined index error in code below
2: How and where do you write the if statement so that if the deletion was successful, it will echo “Session … was Deleted” else if it hasn’t been delete then display echo “An error occured, Session … was not deleted”
Below is the code:
$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";
}
}
?>
Your
undefined indexerror is because you are using$_REQUEST['sessionsDrop']=='', but your form code is –So you should use
$_REQUEST['session']=='', instead of$_REQUEST['sessionsDrop']==''since the super globals ($_POST,$_GET,$_REQUEST) reference thename, not theid.