You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP表单POST变量为空求助:delivery.php向delivery-insert.php传值失败

问题原因及解决方案

兄弟,问题出在你的表单元素上——所有输入框和下拉选择框都没有添加name属性!

核心原因

HTML表单在通过POST/GET提交数据时,只会携带带有name属性的元素的值。id属性仅用于前端DOM操作或样式定位,和表单数据的提交逻辑完全无关。你当前的所有表单控件只有id,没有name,所以PHP的$_POST数组自然无法获取到任何提交的数据。

修复步骤

修改delivery.php中的表单代码,给每个输入框、下拉选择框添加对应的name属性(建议name值和id保持一致,方便对应):

<form action="delivery-insert.php" method="post">
  <div class="form-group">
    <label>Delivery No.</label>
    <input type="text" class="form-control" id="deliverynoinput" name="deliverynoinput" placeholder="D0" required>
    <br>
    <label>Price</label>
    <input type="number" class="form-control" id="deliverypriceinput" name="deliverypriceinput" placeholder="0" required>
    <br>
    <label>Quantity</label>
    <input type="number" class="form-control" id="deliveryquantityinput" name="deliveryquantityinput" placeholder="0" required>
  </div>
  <div class="form-group">
    <label for="selectTruckInput">Select Delivery Truck</label>
    <select class="form-control" id="selectTruckInput" name="selectTruckInput">
      <?php foreach ($trucks as $i => $value) { print "<option>" . $value . "</option>"; } ?>
    </select>
  </div>
  <div class="form-group">
    <label for="selectCustomerInput">Select Delivery Customer</label>
    <select class="form-control" id="selectCustomerInput" name="selectCustomerInput">
      <?php foreach ($customers as $i => $value) { print "<option>" . $value . "</option>"; } ?>
    </select>
  </div>
  <div class="form-group">
    <label for="selectDriverInput">Select Delivery Driver</label>
    <select class="form-control" id="selectDriverInput" name="selectDriverInput">
      <?php foreach ($drivers as $i => $value) { print "<option>" . $value . "</option>"; } ?>
    </select>
  </div>
  <input type="submit"/>
</form>

重要安全提示

你当前的SQL语句直接将用户输入拼接到查询中,存在严重的SQL注入风险,可能导致数据库被恶意篡改或泄露。建议改用MySQLi的预处理语句来执行数据库操作,示例如下:

// 连接数据库部分保持不变
$host = "localhost";
$user = "root";
$pw = "root";
$db = "project";
$con = new mysqli($host, $user, $pw, $db);
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}

// 获取POST数据(现在能正常拿到了)
$dno = $_POST['deliverynoinput'];
$dprice = intval($_POST['deliverypriceinput']);
$dquant = intval($_POST['deliveryquantityinput']);
$dtruck = $_POST['selectTruckInput'];
$dcust = $_POST['selectCustomerInput'];
$ddriver = $_POST['selectDriverInput'];

// 预处理插入delivery表
$stmt = $con->prepare("INSERT INTO delivery (deliveryno, price, truckid, custphoneno, driverid) VALUES (?, ?, ?, ?, ?)");
// bind_param参数说明:s=字符串,i=整数,这里根据你的字段类型调整
$stmt->bind_param("sisss", $dno, $dprice, $dtruck, $dcust, $ddriver);
if ($stmt->execute()) {
    print "added to delivery successfully";
} else {
    print "Error: " . $stmt->error;
}
$stmt->close();

// 预处理插入deliveryquantity表
$stmt = $con->prepare("INSERT INTO deliveryquantity (deliveryno, quantity) VALUES (?, ?)");
$stmt->bind_param("si", $dno, $dquant);
if ($stmt->execute()) {
    print "added to deliveryquantity successfully";
} else {
    print "Error: " . $stmt->error;
}
$stmt->close();

$con->close();

内容的提问来源于stack exchange,提问作者Josh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:37:27