Page 1 of 1

Categoryname isn't added to the database???

Posted: Sun Jan 07, 2007 1:28 am
by cturner
The code that is below is suppose to add a category name (categoryname) as well as a short name (category) for the category and it only adds the short name for the category. The problem is the INSERT INTO only adds category to the database and not the categoryname. I have tested my code to view the categories but the short name only displays. Can someone please have a look at my code and tell me how I can solve this problem? Thanks in advance.

Code: Select all

require "config.php";
$category = mysql_real_escape_string($_POST['category']);
$categoryname = mysql_real_escape_string($_POST['categoryname']);
$arrErrors = array();
if (isset($_POST['addbtn'])) {

if ($shortname = '') {
$arrErrors['category'] = 'Please enter a short name for the category.';
}	

if ($categoryname = '') {
$arrErrors['categoryname'] = 'Please enter a description for the category.';
}

$sql = mysql_query("SELECT `categoryname` FROM `tbl_categories` WHERE 'categoryname' = '$categoryname'") or die ("Could not query because: ".mysql_error());

$check = mysql_num_rows($sql);

if ($check != 0) {
$arrErrors['categoryname'] = 'The category already exists.';
}	

if (count($arrErrors) == 0) {
$result = "INSERT INTO `tbl_categories` (`category`, `categoryname`) VALUES ('$category', '$categoryname')";

if (mysql_query($result)) {
header ('Location: category_added.php');
} else {
print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $result.</p>";
}
} else {
    // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>';
        // Get each error and add it to the error string
        // as a list item.
        foreach ($arrErrors as $error) {
            $strError .= "<li>$error</li>";
        }
        $strError .= '</ul></div>';
}
}

mysql_close();

Posted: Sun Jan 07, 2007 2:00 am
by afbase
are you sure you aren't missing any "}" in your code????
or is that just a snippet of the code? It could just be me not counting curly brackets propberly

Posted: Sun Jan 07, 2007 2:22 am
by volka
Where does $shortname come from? The only occurence in your code is
cturner wrote:if ($shortname = '') {
Thinking this is a typo I'm using $category instead. Missing a = here, comparison is done by ==, = is assigning a value.
Try

Code: Select all

<?php
require "config.php";
error_reporting(E_ALL); ini_set('display_errors', true);
$arrErrors = array();
echo '<pre>Debug POST: '; print_r($_POST); echo "</pre>\n";
if ( isset($_POST['addbtn'], $_POST['category'], $_POST['categoryname']) ) {
	$category = mysql_real_escape_string($_POST['category']);
	$categoryname = mysql_real_escape_string($_POST['categoryname']);

	if ( ''==$category ) {
		$arrErrors['category'] = 'Please enter a short name for the category.';
	}       
	if ( ''==$categoryname ) {
		$arrErrors['categoryname'] = 'Please enter a description for the category.';
	}

	$query = "SELECT
			`categoryname`
		FROM
			`tbl_categories`
		WHERE
			'categoryname' = '$categoryname'";
	$sql = mysql_query($query) or die (mysql_error().': '.$query);

	$check = mysql_num_rows($sql);
	if ($check != 0) {
		$arrErrors['categoryname'] = 'The category already exists.';
	}       

	if (count($arrErrors) == 0) {
		$query = "INSERT INTO
				`tbl_categories`
				(`category`, `categoryname`)
			VALUES
				('$category', '$categoryname')";
		if ( !mysql_query($query) ) {
			$arrErrors['insert'] = mysql_error().': '.$query;
		}
	}
	
	if ( empty($arrErrors) ) {
		header ('Location: category_added.php');
		die();
	}
	else {
		foreach ($arrErrors as $error) {
			echo '<div>', htmlentities($error), "</div>\n";
		}
	}
}
else {
	die('Parameter missing');
}
What does it print?

Posted: Sun Jan 07, 2007 4:27 pm
by cturner
Problem is now solved.