Hello:
My mind went a total blank. I have a script which displays a form and inserts the data from the form into the table.
I added a fourth field and when I run the form the table no longer populates. I can't seem to figure out why. I stripped my script so I could troubleshoot and I still can't seem to find the problem.
My table looks like this:
id int(11) not null auto_increment primary key,
cat_code varchar(15) not null,
cat_name varchar(25) not null,
parent_id int(11) not null default 0
This is my 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'])) {
$cat_code = escape_data($_POST['catcode']);
} else {
$cat_code = FALSE;
echo '<p><font color="red">Please enter a category code!</font></p>';
}
// Check for a category name.
if (!empty($_POST['catname'])) {
$cat_name = escape_data($_POST['catname']);
} else {
$cat_name = FALSE;
echo '<p><font color="red">Please enter a category name!</font></p>';
}
if (!empty($_POST['parentid'])) {
$pid = $_POST['parentid'];
} else {
$pid = 0;
}
if ($cat_code && $cat_name && $pid) { // If everything's OK.
// Add the category to the categories_description table
$query = "INSERT INTO category (cat_code, cat_name, parent_id) VALUES ('$cat_code', '$cat_name', '$pid')";
$result = @mysql_query ($query); // Run the query.
}
} // End of the main submitted conditional.
// --------- DISPLAY THE FORM ---------
?>
<form action="add_category.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><input type="text" name="parentid" size="60" maxlength="60"></p>
</fieldset>
<input type="hidden" name="submitted" value="TRUE" />
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
</form>
The problem lies with the variable $pid which on the form is set to "parentid". The variable $pid should populate the field parent_id in the table.
Here is how the form should work: if a person leaves the Parent Category field blank, the field parent_id should default to 0 otherwise insert the value entered into the form into parent_id.
Well, when I try to run the form with the last form field blank, nothing gets inserted into the table. I can't seem to figure out why.
Can someone take a look and help me? I could use a fresh pair of eyes. I am sure that the solution is probably something very basic. As I said earlier, my mind went a complete blank.
Thank you in advance for your help.