Jump to content


Photo

Checking if a row exists


  • Please log in to reply
3 replies to this topic

#1 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 01 April 2006 - 06:09 PM

<?php
require_once "maincore.php";
require_once "subheader.php";
require_once "side_left.php";
opentable('Add Category');
$owner = $userdata['user_name'];
$categoryname = addslashes(strip_tags($_POST['catname']));
$description = addslashes(strip_tags($_POST['description']));
$status = $_POST['status'];
$date = date('F d, Y');

    $idq = mysql_query("SELECT `id` FROM `registered_cat` ORDER BY `id` DESC LIMIT 1");
    $ida = mysql_fetch_assoc($idq);
    $id = $ida['id'] + 1; 

$sql2 = mysql_query("SELECT '$categoryname' FROM registered_cat WHERE owner='$username'");
$result2 = mysql_num_rows($sql2);
    
if($result2=="0"){
echo "Category Exists Already!!<br/>";
exit;
} else {
MYSQL_QUERY("INSERT INTO registered_cat (`owner`,`cat_name`,`cat_description`,`status`,`date`,`id`) VALUES ('$owner','$categoryname','$description','$status','$date','$id')");
   echo "Category Added! <a href='viewpage.php?page_id=7'>Click here to go back.</a>";
}

require_once "footer.php";
?>


Even though a row does not exist, it says it is. What am I doing wrong here? Help would be greatly appreciated!

Regards,
AIMMultimedia.com

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 April 2006 - 06:35 PM

You're "if" condition is wrong. You have:
<?php
if($result2=="0"){
echo "Category Exists Already!!<br/>";
exit;
?>
it should be:
<?php
if($result2 > 0){
echo "Category Exists Already!!<br/>";
exit; ?>

Ken


#3 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 01 April 2006 - 07:50 PM

I did what you said but it gives me the same error.

<?php
require_once "maincore.php";
require_once "subheader.php";
require_once "side_left.php";
opentable('Add Category');
$owner = $userdata['user_name'];
$categoryname = addslashes(strip_tags($_POST['catname']));
$description = addslashes(strip_tags($_POST['description']));
$status = $_POST['status'];
$date = date('F d, Y');

    $idq = mysql_query("SELECT `id` FROM `registered_cat` ORDER BY `id` DESC LIMIT 1");
    $ida = mysql_fetch_assoc($idq);
    $id = $ida['id'] + 1; 



$sql2 = mysql_query("SELECT '$categoryname' FROM registered_cat WHERE owner='$owner'");
$result2 = mysql_num_rows($sql2);
    
if($result2 > 0){
  opentable('Category Exists!');  
echo "Category Exists Already!! <a href='viewpage.php?page_id=7'>Click here to go back.</a><br/>";

} else {
  opentable('Category Added!');  
MYSQL_QUERY("INSERT INTO registered_cat (`owner`,`cat_name`,`cat_description`,`status`,`date`,`id`) VALUES ('$owner','$categoryname','$description','$status','$date','$id')");
   echo "Category Added! <a href='viewpage.php?page_id=7'>Click here to go back.</a>";
}

require_once "footer.php";
?>

Could it be the mysql query?

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 01 April 2006 - 07:59 PM

Use backtick marks and not single quotes on the query. You then must check for errors before executing any subsequent MySQL commands. Example

$sql2 = mysql_query("SELECT `$categoryname` FROM registered_cat WHERE `owner` = '$owner'") or die('Error: ' . mysql_error());

$result2 = mysql_num_rows($sql2);
.
.
.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users