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
https://forums.phpfreaks.com/topic/6343-checking-if-a-row-exists/
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?
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.