Hello:
I have a form which contains a drop down box. The values from the drop down box is retrieved from a table. The script runs but the drop down box is empty.
The fields in the table are:
category_id int not null auto_increment primary key
category_code
category_name
parent_id
The drop down box should show the data in the category_name field. The option value is set to the category_id which later will be inserted into the table under the parent_id field.
Can someone help me out? Thank you in advance.
This is the script:
<?php #
require_once ('mysql_connect.php'); // Connect to the database.
if (isset($_POST['submitted'])) { // Handle the form.
// Check for a category code.
if (!empty($_POST['catcode'])) {
$catcode = escape_data($_POST['catcode']);
} else {
$catcode = FALSE;
echo '<p><font color="red">Please enter a category code!</font></p>';
}
// Check for a category name.
if (!empty($_POST['catname'])) {
$catname = escape_data($_POST['catname']);
} else {
$catname = FALSE;
echo '<p><font color="red">Please enter a category name!</font></p>';
}
if (!empty($_POST['parentname'])) {
$parentname = $_POST['parentname'];
} else {
$parentname = 0;
}
if ($catcode && $catname) { // If everything's OK.
$query = "INSERT INTO category (category_code, category_name, parent_id)
VALUES ('$catcode', '$catname', '$parentname')";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
if (mysql_affected_rows() == 1) { // If it ran OK.
// Finish the page.
echo '<h3>Category Added!</h3>';
}
}
else {
echo '<p><font color="red">Submission could not be completed. Please try again!</font></p>';
}
}
?>
<form action="add_category_2.php" method="post">
<fieldset><legend>Fill out the form to add a category:</legend>
<p><b>Category Code:</b> <input type="text" name="catcode" size="60" maxlength="60"></p>
<p><b>Category Name:</b> <input type="text" name="catname" size="60" maxlength="60"></p>
<p><b>Parent Category:</b>
<select name="parentname">
<?php
$query = "SELECT * FROM category ASC";
$result = @mysql_query($query);
while($row = mysql_fetch_array($result)) {
$category_id = $row['category_id'];
$category_code = $row['category_code'];
$category_name = $row['category_name'];
$parent_id = $row['parent_id'];
echo "<option value=$category_id>$category_name</option>";
}
echo "</select>";
?>
</fieldset>
<input type="hidden" name="submitted" value="TRUE" />
<div align="center"><input type="submit" name="submitted" value="Submit" /></div>
</form>