Jump to content

Checking if a row exists


lpxxfaintxx

Recommended Posts

[code]<?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";
?>[/code]


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
Link to comment
Share on other sites

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

Ken
Link to comment
Share on other sites

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

[code]<?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";
?>
[/code]

Could it be the mysql query?
Link to comment
Share on other sites

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

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

$result2 = mysql_num_rows($sql2);
.
.
.
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.