I'm working on a site that allows the user to submit some classes. Each class is listed under a certain category.
The problem I'm having is in the script that allows new categories to be submitted to the MySQL database. It's very simple, just a category name and description taken from a form and inserted into the database. Here's the full script in question:
Code: Select all
<!-- Begin Header -->
<?php
//Set the page title
$pageTitle = 'Add Category';
//Set the active tab
$activeTab = 'categories';
//Include the header HTML
include_once ('admin_template_front_header.html');
?>
<!-- End Header -->
<!-- Begin Content -->
<?php // This allows users to add categories to the database.
// Connect to the database.
require_once ('mysql_connect.php');
if (isset($_POST['submit'])) { // Handle the form.
// Check for a category name. (REQUIRED)
if (!empty($_POST['catName'])) {
$n = escape_data($_POST['catName']);
} else {
$n = FALSE;
echo '<p>You forgot to enter a category name.</p>';
}
// Check for a category description (NOT REQUIRED).
if (!empty($_POST['catDesc'])) {
$d = escape_data($_POST['catDesc']);
} else {
$d = '';
}
if ($u) { // If the required field is submitted.
// Add the category to the categories table.
$query = "INSERT INTO categories (category_name, category_description) VALUES ('$n', '$d')";
$result = @mysql_query ($query); // Run the query.
$cid = @mysql_insert_id(); // Get the category ID.
if ($cid > 0) { // If it ran OK.
if ($result) {
echo '<p>The new category was added to the database</p>';
$_POST = array();
}
else // If it did not run OK.
echo '<p>The category could not be added to the database due to a system error.</p>';
}
} else { // If one of the data tests failed.
echo '<p><font color="red">Please try again.</font></p>';
}
} // End of the main Submit conditional.
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<h1>Add a New Category</h1>
<p>Fill in the following form. The description should be as generic as possible. Remember, categories should fit multiple classes, not a specific one. When you are done, click "next" to add a new category to the database.</p>
<fieldset><legend>Category Info</legend>
<label>Name: <input type="text" title="Category Name" name="catName" id="catName" tabindex="1" /></label>
<label>Description: <textarea title="Category Description" name="catDesc" id="catDesc" tabindex="2" rows="8" cols="32"></textarea></label>
</fieldset>
<input type="submit" name="submit" value="Submit" />
</form>
<!-- End Content -->
<!-- Begin Footer -->
<?php
mysql_close(); // Close the database connection.
include ('template_front_footer.html'); // Include the HTML footer.
?>
<!-- End Footer -->Like I said, I'm very new to this, but I would appreciate it if anyone could let me know what's going on. I've been able to figure out all the problems in the little bits of code I've done, but this has me very frustrated. It's probably something really small.
Oh, and any other comments on the code are welcome. I'm definately still learning.
Anyway, thanks you so much.