Php 未提交到post数组的下拉列表值
目前正在处理一个php项目,其中我连接到phpmyadmin中的数据库。我目前正在实现一对多关系,在我的一个表单上有一个下拉列表,其中包含数据库中产品可以拥有的类别(外键)列表,但是当我检查post数组和包含插入查询所有值的数组时,除了外键外,所有内容都在其中 下拉列表和值数组:Php 未提交到post数组的下拉列表值,php,html,mysql,arrays,Php,Html,Mysql,Arrays,目前正在处理一个php项目,其中我连接到phpmyadmin中的数据库。我目前正在实现一对多关系,在我的一个表单上有一个下拉列表,其中包含数据库中产品可以拥有的类别(外键)列表,但是当我检查post数组和包含插入查询所有值的数组时,除了外键外,所有内容都在其中 下拉列表和值数组: <select name="Category_ID"> <?php foreach ($category as $cat) { echo '<option valu
<select name="Category_ID">
<?php
foreach ($category as $cat) {
echo '<option value="' . $cat['ID'] . '">' . $cat['ID'] . '</option>';
}
?>
</select>
Array (
[authorName] => Hiroshi Sakurazaka
[bookName] => All You Need Is Kill
[costPrice] => 59
[sellPrice] => 99
[productCatID] => )
Could not insert book
有人能告诉我我缺少什么吗?对于foreach,您必须明确请求访问密钥,因此如果不请求,您只能获取数组值 只需执行以下操作即可进行调试(在
之外):
我想你会看到你需要的实际数据在哪里
(此外,您在运行时似乎没有出现严格的错误和注意事项,这对于调试至关重要,并且当您尝试访问的数组键不存在时,可能会向您显示一些注意事项)对于foreach,您必须显式请求访问该键,因此如果不存在,则只能获取数组值 只需执行以下操作即可进行调试(在
之外):
我想你会看到你需要的实际数据在哪里
(此外,您似乎在运行时没有出现严格的错误和注意事项,这对调试至关重要,并且当您尝试访问的数组键不存在时,可能会向您显示一些注意事项)您选择的名称为
Category\u ID
而不是productCatID
。如果您希望在productCatID
下获取/发布数据,则需要将您的选择命名为productCatID您的选择的名称为类别ID
而不是productCatID
。如果您希望在productCatID
下获取/发布数据,则需要命名您的selectproductCatID
最终解决了我的问题,因此我将发布我是如何做到的。为了调试我的代码并查看哪些值被传递到$POST
数组和$formdata
数组中,我使用print\r
来发布每个数组(如果有问题),我得到的结果如下:
正如您可以看到的那样,$POST
数组从下拉列表中获得的值很好,问题出在表单数据数组上。令人尴尬的是,这个问题只是一个简单的打字错误,在我的验证脚本中很快得到了解决 终于解决了我的问题,所以我将发布我是如何解决的。为了调试我的代码并查看哪些值被传递到$POST
数组和$formdata
数组中,我使用print\r
来发布每个数组(如果有问题),我得到的结果如下:
正如您可以看到的那样,$POST
数组从下拉列表中获得的值很好,问题出在表单数据数组上。令人尴尬的是,这个问题只是一个简单的打字错误,在我的验证脚本中很快得到了解决 您是否正确地使用POST/GET参数“Category_ID”(它们有不同的名称)填充数组项“productCatID”?是的,它们正确地填充了显示在数据库中进行保存的代码以及任何相关内容。从你目前发布的内容来看,它没有给出任何问题的线索。你是否正确地用POST/GET参数“Category_ID”填充数组项“productCatID”(它们有不同的名称)?是的,它们正确地填充了显示在数据库中进行保存的代码以及任何相关的内容。从你目前发布的内容来看,它没有给出任何关于问题的线索。我补充说,对于每个问题,当我这样做时,只有一个下拉选项可用,你能解释一下严格的错误通知是什么意思吗?我假设您需要x-debug之类的东西来调试php代码。在php.ini中,有一个指令错误报告。将E_ALL添加到该指令中,您应该开始看到您的错误。@Jambo只要您使用的是php 5.4+版本,就可以获得所有信息,是的。(需要那些未初始化的数组键通知以获得早期检测帮助)我补充说,对于每一个,当我这样做时,只有一个下拉选项可用,您能否解释严格的错误通知是什么意思?我假设您需要x-debug之类的东西来调试php代码。在php.ini中,有一个指令错误报告。将E_ALL添加到该指令中,您应该开始看到您的错误。@Jambo只要您使用的是php 5.4+版本,就可以获得所有信息,是的。(需要那些未初始化的数组键通知以获得早期检测帮助)我在一段时间前更改了此项,错误仍然存在。我在一段时间前更改了此项,错误仍然存在
<?php
require_once 'product.php'; //Connecting to the product class
require_once 'Classes/productTable.php'; //Connecting to the TableGateway
require_once 'Classes/Connection.php'; //Connecting to the Connection class
require_once 'validateProduct.php';//Connecting to the product validation
require_once 'utils/functions.php';
//start_session();
//
//if (!is_logged_in()) {
// header("Location: login_form.php");
//}
echo '<pre>';
print_r($_POST);
echo '</pre>';
$formdata = array();
$errors = array();
validate($formdata, $errors);
if (empty($errors)) {
$AuthorName = $formdata['AuthorName'];
$BookName = $formdata['BookName'];
$costPrice = $formdata['CostPrice'];
$sellPrice = $formdata['sellPrice'];
$productCatID = $formdata['productCatID'];
echo '<pre>';
print_r($formdata);
echo 'Form Data array';
echo '</pre>';
$connection = Connection::getInstance();
$gateway = new productTable($connection);
$id = $gateway->insert($AuthorName, $BookName, $costPrice, $sellPrice, $productCatID);
header('Location: viewProducts.php');
}
else {
require 'createProductForm.php';
}
> public function insert($authorName, $bookName, $costPrice, $sellPrice,
> $productCatID) {
> $sql = "INSERT INTO "
> . "`product`(`AuthorName`, `BookName`, `CostPrice`, `sellPrice`, `productCatID`)"
> . " VALUES (:authorName,:bookName,:costPrice,:sellPrice,:productCatID)";
> $statement = $this->connection->prepare($sql);
> $params = array(
> "authorName" => $authorName,
> "bookName" => $bookName,
> "costPrice" => $costPrice,
> "sellPrice" => $sellPrice,
> "productCatID" => $productCatID
> );
> print_r($params);
> $status = $statement->execute($params);
>
> if (!$status) {
> die("Could not insert book");
> }
>
> $id = $this->connection->lastInsertId();
>
> return $id; }
foreach($category as $key=>$cat){
var_dump($cat['ID'], $cat, $key);
}
$POST Array
(
[AuthorName] => g
[BookName] => g
[CostPrice] => 33
[sellPrice] => 3
[productCatID] => 4
[createProduct] => Create Product
)
form data array
(
[AuthorName] => g
[BookName] => g
[CostPrice] => 33
[sellPrice] => 3
[ProductCatID] =>
)