$ _POST数组问题PHP MySQL

First of all, I am a newbie when it comes to coding, so please be kind and patient :)

What I am trying to do is to select two rows ('ID', 'name') from a MySQL table (categories), populate a drop down list with one row ('name'), and on submission of a form, pass the other ('ID') to another table.

Now, I can populate the drop down list, no problem. I have populated this with both 'ID' and 'name' to test that both of the variables I am using to hold this information, contain the correct data. But I cannot seem to $_POST the information.

I guess I am either looking at the wrong part of the array, or I am simply using the wrong code.

This is the code to create a new product, under a category from the database.

<?php

include 'db_config.php';

?>

<form enctype="multipart/form-data" action="insert.php" method="post">
<h3>Add New Product</h3>
Category:

<!-- START OF categories (table) names (row) SQL QUERY -->

<? $sql = "SELECT ID, name FROM categories";
$result = $mysqli->query($sql);

echo "<select name='category_name'>";
while ($row = $result->fetch_assoc()) {

    $cat_ID=$row['ID'];
    $cat_name=$row['name'];
    extract($row);
    echo "<option value='" . $cat_ID . $cat_name . "'>" . $cat_ID . " " . $cat_name ."</option>";
}
echo "</select>";

?>

<!--END OF SQL QUERY -->

<br>
Code: <input type="text" name="code"><br>
Name: <input type="text" name="prod_name"><br>
Description: <input type="textarea" name="description"><br>
Image: <input type="file" name="image"><br>
<input type="Submit">
</form>

For now, I am just echoing this out in the insert.php script, to test the code above. This is a snippet of the insert.php script.

echo "ID: " .  $_POST['$row["ID"]'] . "<br>";
echo "Category: " .  $_POST['$row["name"]'] . "<br>";
echo "Code: ". $_POST['code'] . "<br>";
echo "Name: " . $_POST['prod_name'] . "<br>";
echo "Description: ". $_POST['description'] . "<br>";
echo "Image: " . $_POST['image'] . "<br>";

Don't worry about the last line above. I know this needs to be $_FILES, and I have all this covered. I have stopped writing the data to the table until I get my issue fixed. In the full script, image are being upload to "/images" and the location stored in the table. This all works fine.

The problem is with the first two lines, as they are blank when returned. I thought I was storing the information correctly, as I am calling the same variables to populate the drop down list, but I cannot seem to $_POST it.

Does that makes sense?

Thanks to all who help me. Once day I will be as good as you....I hope.

TIA

Smurf.

The $_POST variable you want, is inside category_name

Cuz your select is...

<select name='category_name'>

So you need to get it by...

$_POST['category_name'];

Which will return whatever you've assigned to the select options...ie

2  Name

2 being the ID, and Name being the name

But if you then want to use that ID to retrieve from DB or anything, you're gonna have to explode that apart...like so....to get each piece.

$array = explode(" ", $_POST['category_name']);

That will leave you with...

  $array[0] = ID
  $array[1] = Name

But I would avoid all that part, by just assigning the ID to the value only...like so..

echo "<option value = '".$cat_ID."'> ".$cat_name." </option>";

That way you just pass the ID and have access to it on the other side.

this bellow:

echo "ID: " .  $_POST['$row["ID"]'] . "<br>";
echo "Category: " .  $_POST['$row["name"]'] . "<br>";

is wrong, select element has its name category_name, so, instead of this, you should do:

echo "Category: " .  $_POST['category_name'] . "<br>";
echo "ID: " .  $_POST['$row["ID"]'] . "<br>";
echo "Category: " .  $_POST['$row["name"]'] . "<br>";

There aren't any form elements with those names in your form ($row["ID"] and $row["name"]). Those would be really strange names for a form element anyway. The form element you're creating is:

<select name='category_name'>

So the selected value would be posted as:

$_POST['category_name']

The option elements for that select appear to have values which are a combination of ID and Name:

"<option value='" . $cat_ID . $cat_name . "'>"

Thus, if the user selects an option with a value of 1SomeName then $_POST['category_name'] will evaluate to '1SomeName'.

It's certainly unconventional to use the combination of ID and Name for the option values, but it should work. The problem is presents is that you now have a composite string which needs to be parsed in order to be useful. Generally what one would do is just use the ID as the value and the Name as the display. All you should need to use it throughout the code is the ID.