Page 1 of 1

[SOLVED] What's wrong with this?

Posted: Tue Aug 31, 2004 6:03 pm
by brillo
Hi everyone. Complete PHP newbie, so please excuse me. :)

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 -->
I test the script(just try it out on my server) and get one of my error messages. Specifically the "Please try again." error, and nothing is added to the database. I shouldn't be getting that error (I don't want to be getting it at least.)

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.

Posted: Tue Aug 31, 2004 6:07 pm
by markl999
if ($u) {
What's $u ?
You use $_POST['whatever'] elsewhere so are you sure you don't mean $_POST['u'] where you are using $u?
Also see viewtopic.php?t=17096 for some debugging tips you can use to locate the problem.

Posted: Tue Aug 31, 2004 6:09 pm
by tim
i dont see where the var $u is defined?

Also, you can echo out the query to see if the variables are being past and make sure everything is okay.

or die(mysql_error()); is also a good troubleshooting function.

edit - nvm, mark nailed it all before me

back in black I see shooter :wink:

Posted: Tue Aug 31, 2004 6:22 pm
by brillo
markl999 wrote:
if ($u) {
What's $u ?
You use $_POST['whatever'] elsewhere so are you sure you don't mean $_POST['u'] where you are using $u?
Also see viewtopic.php?t=17096 for some debugging tips you can use to locate the problem.
Ack. :p Thank you. :)

Code: Select all

if ($u)

Was supposed to be :

Code: Select all

if ($n)
I want it to check if the category name ($n) is present. $u was from a different script that added class info to the database. I copied the script and didn't fully change it how it should have been changed. Just changed it now though, and it seems to work fine. Thanks.

And thank you for the link to the general debugging tips.