【问题标题】:drop down value not being submitted to post array下拉值未提交到 post 数组
【发布时间】:2016-07-01 00:53:41
【问题描述】:

目前正在处理一个 php 项目,我在该项目中连接到 phpmyadmin 中的数据库。我目前正在实现我的一对多关系,并且在我的一个表单上有一个下拉列表,其中包含我的数据库中的产品可以拥有的类别列表(外键),但是当我检查帖子数组时,包含插入查询的所有值的数组,除了外键之外,一切都在那里。

下拉列表和值数组:

 <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

这是将formdata数组中的数据转换为对象的文件:

<?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; }

谁能告诉我我错过了什么?

【问题讨论】:

  • 您是否使用 POST/GET 参数“Category_ID”正确填充数组项“productCatID”(它们具有不同的名称)?
  • 是的,他们填写正确
  • 向我们展示在数据库中保存的代码以及相关的任何内容。从您到目前为止发布的内容来看,它没有提供任何关于问题所在的线索。

标签: php html mysql arrays


【解决方案1】:

使用 foreach 您必须明确请求访问密钥,因此如果您不这样做,您将只能获得数组值。

只需执行此操作即可调试(&lt;select&gt; 之外):

foreach($category as $key=>$cat){
    var_dump($cat['ID'], $cat, $key);
}

我想你会看到你需要的实际数据在哪里。

(此外,您似乎在运行时没有严格的错误和通知,这对于调试至关重要,并且可能会在您尝试访问的数组键不存在时向您显示一些通知)

【讨论】:

  • 我为每一个都添加了,当我这样做时,只有一个下拉选项可用,您能否解释一下您所说的严格错误通知是什么意思?我假设你需要像 x-debug 这样的东西来调试 php 代码
  • 不,在 php.ini 中有一个指令 error_reporting。将 E_ALL 添加到该指令中,您应该会开始看到错误。
  • @Jambo 只要您使用 php 版本 5.4+,就可以得到一切,是的。 (需要那些未初始化的数组键通知以提供早期检测帮助)
【解决方案2】:

您的选择名称为 Category_ID 而不是 productCatID。如果您希望在 productCatID 下输入 GET/POST 数据,您需要将您的选择命名为 productCatID

【讨论】:

  • 我前段时间改了,还是报错
【解决方案3】:

终于解决了我的问题,所以我会发布我是如何做到的。为了调试我的代码并查看将哪些值传递到 $POST 数组和 $formdata 数组中,如果出现问题,我使用 print_r 发布每个数组,这就是我得到的结果:

$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] => 
    )

正如您所见,$POST 数组从下拉列表中获取值很好,问题在于表单数据数组。令人尴尬的是,这个问题只是一个简单的拼写错误,在我的验证脚本中很快得到解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多