首页 > 解决方案 > PHP 表单 POST 变量为空

问题描述

我正在尝试使用 HTTP POST 传递delivery.php变量delivery-insert.php。提交时,不传入任何变量。

下面是中的表格delivery.php

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

下面是delivery-insert.php我尝试收集 POST 变量的地方。

<html>
<body>

<?php
$host = "localhost";
$user = "root";
$pw = "root";
$db = "project";

$con = new mysqli($host, $user, $pw, $db);

if (! $con) {
    die("Connection failed: " . mysqli_connect_error());
}

$dno = $_POST['deliverynoinput'];
$dprice = intval($_POST['deliverypriceinput']);
$dquant = intval($_POST['deliveryquantityinput']);
$dtruck = $_POST['selectTruckInput'];
$dcust = $_POST['selectCustomerInput'];
$ddriver = $_POST['selectDriverInput'];

print '<h1>Here</h1>';
print '<h1>'.$dno.'</h1>';
print $_POST['selectTruckInput'];

$sql = "INSERT INTO delivery (deliveryno, price, truckid, custphoneno, driverid) VALUES ('$dno','$dprice','$dtruck','$dcust','$ddriver')";

if (mysqli_query($con, $sql)) {
    print "added to delivery successfully";
} else {
    print "Error: " . mysqli_error($con);
}

$sql = "INSERT INTO deliveryquantity (deliveryno, quantity) VALUES ('$dno', '$dquant')";

if (mysqli_query($con, $sql)) {
    print "added to deliveryquantity successfully";
} else {
    print "Error: " . mysqli_error($con);
}

$con->close();

?> 
</body>
</html>

有谁知道为什么我所有的变量都是空的?

标签: phphtmlmysql

解决方案


您将需要输入的属性名称。这样,浏览器将发送相应的名称作为$_POST. 这甚至适用于数组!

例如

<!-- Single Value -->
<input name="someValue" />

<!-- Array -->
<input name="someArray[]" />
<input name="someArray[]" />

可以这样访问:

// Single value
$_POST['someValue']

// Array
$_POST['someArray'][$numericIndex]

推荐阅读