drfcaw7460 2012-10-19 00:23
浏览 32
已采纳

尝试从下拉菜单中删除会话时发生未定义的索引错误

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";
}
}
    ?>
  • 写回答

1条回答 默认 最新

  • duandangqin0559 2012-10-19 05:09
    关注

    Your undefined index error is because you are using $_REQUEST['sessionsDrop']=='', but your form code is -

    $sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;
    

    So you should use $_REQUEST['session']=='', instead of $_REQUEST['sessionsDrop']=='' since the super globals ($_POST, $_GET, $_REQUEST) reference the name, not the id.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接