HTML表单提交到MySQL时报Undefined array key及Session取值问题排查
问题排查与修复方案
核心问题1:表单未正常提交POST数据
你当前使用<a>标签作为提交按钮,哪怕设置了type="submit",<a>标签的默认行为是跳转访问href属性的链接,走GET请求,根本不会提交表单内的POST数据,所以order_save.php里拿不到$_POST['item']、$_POST['quantity']这些值。
核心问题2:Session取值异常
$_SESSION['username']不存在有两种可能:
- 用户未经过登录流程直接访问
order_save.php,Session未初始化赋值 - 登录成功跳转到
Startseite.php后,后续页面没有正确开启Session读取
其他可优化的代码问题
<script>标签的引入属性写错,是src不是scr<table>标签不需要设置src属性,该属性对表格元素无效
修复代码
HTML部分修正
把原来的a标签提交按钮换成表单原生提交按钮,修正script引入错误:
<form method="POST" action="order_save.php"> <table class="u-table-entity"> <!-- 修正script引入的属性错误 --> <script src="calculation.js"></script> <colgroup> <col width="20%"> <col width="2.1%"> <col width="22%"> <col width="21.7%"> <col width="34.2%"> </colgroup> <tbody class="u-table-alt-grey-5 u-table-body"> <tr style="height: 55px;"> <th class="u-table-cell u-table-cell-1"><b>Produkt</b><span style="font-weight: 700;"></span></th> <th class="u-table-cell"></th> <th class="u-table-cell u-table-cell-3"><b>Einzelpreis</b></th> <th class="u-table-cell u-table-cell-4"><b>Menge</b></th> <th class="u-table-cell u-table-cell-5"><b>Gesamtpreis</b></th> </tr> <tr style="height: 55px;"> <td class="u-table-cell"> <input type="text" class="item" value="Kornspitz" name="item" placeholder="Kornspitz" readonly /> </td> <td class="u-table-cell"></td> <td class="u-table-cell"> <input type="text" class="price" value="1.39" name="price" readonly/> </td> <td class="u-table-cell"> <input type="text" class="quantity" name="quantity"/> </td> <td class="u-table-cell"> <input type="text" class="total" name="total" readonly/> </td> </tr> <tr style="height: 55px;"> <td class="u-table-cell"></td> <td class="u-table-cell"></td> <td class="u-table-cell"></td> <td class="u-table-cell u-table-cell-19"> Gesamtpreis:</td> <td class="u-table-cell"></td> </tr> <tr style="height: 56px;"> <td class="u-table-cell"></td> <td class="u-table-cell"></td> <td class="u-table-cell"></td> <td class="u-table-cell u-table-cell-24"> <a class="u-active-none u-border-none u-btn u-button-link u-button-style u-hover-none u-none u-text-custom-color-1 u-text-hover-custom-color-2 u-btn-1" href="https://nicepage.com">AGB</a> </td> <td class="u-table-cell u-table-cell-25"> <!-- 替换a标签为原生submit按钮,可复用原有样式类 --> <button class="u-border-none u-btn u-button-style u-custom-color-1 u-hover-custom-color-2 u-btn-2" type="submit">BESTELLUNG</button> </td> </tr> </tbody> </table> </form>
order_save.php部分修正
增加Session存在性判断,避免未登录访问报错,同时增加POST参数校验和SQL注入防护:
<?php session_start(); // 未登录用户直接跳转到登录页 if (!isset($_SESSION['username'])) { header("Location: 你的登录页地址.php"); exit(); } // 校验表单提交参数完整性 if (!isset($_POST['item'], $_POST['quantity'])) { die("提交的表单数据不完整"); } $con= mysqli_connect('localhost','root','123456'); mysqli_select_db($con, 'orders'); // 参数转义避免SQL注入风险 $username = mysqli_real_escape_string($con, $_SESSION['username']); $item = mysqli_real_escape_string($con, $_POST['item']); $quantity = mysqli_real_escape_string($con, $_POST['quantity']); // 后续数据库写入逻辑 ?>
内容的提问来源于stack exchange,提问作者Setzi138
相关产品推荐
相关产品推荐

