cturner Posted November 27, 2006 Share Posted November 27, 2006 When I test my code that is below it keeps on displaying 'the category already exists' regardless of whether the category exists or not. Can someone please have a look at my code and tell me why this is happening? Thanks in advance. ???[code=php:0]require "config.php";$shortname = mysql_real_escape_string($_POST['shortname']);$categoryname = mysql_real_escape_string($_POST['categoryname']);$arrErrors = array();if (isset($_POST['addbtn'])) { if ($shortname = '') { $arrErrors['shortname'] = 'Please enter a short name for the category.'; } if ($categoryname = '') { $arrErrors['categoryname'] = 'Please enter a description for the category.'; } $sql = mysql_query("SELECT * FROM tbl_articles 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_articles` (`id`, `category`, `categoryname`) VALUES (0, '$shortname', '$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>'; }}[/code] Link to comment https://forums.phpfreaks.com/topic/28597-the-category-already-exists/ Share on other sites More sharing options...
genericnumber1 Posted November 27, 2006 Share Posted November 27, 2006 try if ($check !== false) {mysql_num_rows returns false on failure...That's just my guess... I'm not sure[b]Edit:[/b] That's probably not it, it's getting late and I'm losing my think powah, I'll look at it some more.... Link to comment https://forums.phpfreaks.com/topic/28597-the-category-already-exists/#findComment-130841 Share on other sites More sharing options...
JasonLewis Posted November 27, 2006 Share Posted November 27, 2006 with this:[code=php:0]if ($check != 0) {$arrErrors['categoryname'] = 'The category already exists.';}[/code]i always do > 0 for some reason, try that. but after it echo something like 'gets here' then exit(); the script and add one to see if it is getting there. also, run the query in phpmyadmin to see what it returns there. Link to comment https://forums.phpfreaks.com/topic/28597-the-category-already-exists/#findComment-130845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.