php - drop down value not being submitted to post array


Currently working with a php project in which I connect to a database in phpmyadmin. I'm currently implementing my one to many relationship and on one of my forms there is a drop down list with the list of categorys(foreign key) that a product in my database can have, however when I check the post array, and the array that contains all the values for the insert query everything is there except the foreign key.

Drop down list and array of values:

 <select name="Category_ID">
     foreach ($category as $cat) {
     echo '<option value="' . $cat['ID'] . '">' . $cat['ID'] . '</option>';

     Array ( 
    [authorName] => Hiroshi Sakurazaka 
    [bookName] => All You Need Is Kill 
    [costPrice] => 59
    [sellPrice] => 99 
    [productCatID] => ) 
    Could not insert book

Heres the file that converts the data in the formdata array into an object:

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';

//if (!is_logged_in()) {
//    header("Location: login_form.php");

    echo '<pre>';
    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>';
    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';

Heres the function in the table gateway that inserts the object into the database:

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

can somebody please tell me what I'm missing?




Your select has the name ofCategory_ID notproductCatID. If you expecting GET/POST data coming in underproductCatID you need to name your selectproductCatID.




Solved my problem finally, so I'll post how I did it. For debuging my code and to see what values were being passed into the$POST array and the$formdata array, I usedprint_r to post each array if there was a problem and heres what I got:

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

As you can see the$POST array was getting the value from the drop down list just fine, it was the form data array that was the issue. Embarrassingly the issue was just a simple typo error that was quickly resolved in my validation script.




With foreach you have to explicitly request access to the key, so if you don't, you'll only get the array values.

Just do this to debug (outside of the<select>):

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

And I think you'll see where the actual data you need is.

(also, you seem to be running without strict errors and notices on, which is crucial for debugging and might show you some notices when array keys you try to access don't exist)

People are also looking for solutions to the problem: html - Display search results on the same page in a table with PHP


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.