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 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题