php - 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>
有谁知道为什么我所有的变量都是空的?
解决方案
您将需要输入的属性名称。这样,浏览器将发送相应的名称作为$_POST
. 这甚至适用于数组!
例如
<!-- Single Value -->
<input name="someValue" />
<!-- Array -->
<input name="someArray[]" />
<input name="someArray[]" />
可以这样访问:
// Single value
$_POST['someValue']
// Array
$_POST['someArray'][$numericIndex]
推荐阅读
- php - 使用 Laravel 在服务器上下载图像不起作用
- python-3.x - 标签中的 Python 3 Tkinter 自定义字体
- influxdb - 在 Platfrom.sh 上访问 InfluxDB
- javascript - React Native CouchDB/pouchDB 应用程序在发布 apk 中不起作用
- android - 裁剪使用毕加索获取的图像?
- html - CSS:我想创建带有渐变颜色系统的加载器
- android-studio - 在 Android Studio 3.6+ 中默认设置 XML 拆分视图?
- postgresql - 函数不在游标中重新运行查询
- amazon-web-services - AWS Lambda $LATEST 版本的定义
- azure - 如何限制 Azure 中的活动目录权限?