Click to See Complete Forum and Search --> : Trouble verifying if a record exists


youthBYTES
11-01-2006, 11:11 AM
i know this is something people commonly ask and there is plenty of examples, but i don't know WHY but its not working for me, help?


$posttime = time();
$type = trim($_POST['type']);
$title = trim($_POST['title']);
$details = addslashes(trim($_POST['details']));
$filecount = $_POST['filecount'];
$result = $DB->query("SELECT * FROM `portfolio` WHERE type='$type' AND title='$title'");
if($row = mysql_fetch_array($result))
{
$exists = 'yes';
}
// Verification
if (cms_isEmpty($title) == false || cms_isEmpty($details) == false || cms_isEmpty($exists))
{
$submitdetails = $DB->query("INSERT INTO `portfolio` (`date`,`type`, `title`, `details`) VALUES ('$posttime','$type','$title','$details')");

$uploaddir = '../images/' . $type . '/';
for ($i =0; $i<$filecount; $i++)
{
$filename = $_FILES['file']['name'][$i];
$filetmp = $_FILES['file']['tmp_name'][$i];
$filepath = $uploaddir . $filename;
$dbname = 'image' . ($i+1);
if ($filename != "")
{
if (!file_exists($filepath))
{
$upload = move_uploaded_file($filetmp, $filepath);
$submitimages = $DB->query("UPDATE portfolio SET $dbname='$filename' WHERE title='$title'");
echo $filename . ' <i>uploaded sucessfully...</i><br />';
}
else
{
echo $filename . ' <i>already exists...</i><br />';
}
}
}
cms_redirect('index.php','Your new portfolio work was successfully entered into the database!<br />You will now be taken to the main administration page.');
}
else
{
if (cms_isEmpty($title))
{
cms_msgBox('You need a title');
$action = 'add';
}
if (cms_isEmpty($details))
{
cms_msgBox('You need details');
$action = 'add';
}
if (cms_isEmpty($exists) == false)
{
cms_msgBox('Portfolio already exists');
$action = 'add';
}
}

netbuddy
11-01-2006, 11:16 AM
Yes we have telepathic powers, so we shall answer likewise ...........



See it?




Ok... I assume that you have actually conected to the Database?

NogDog
11-01-2006, 11:51 AM
Please define the problem: What is it not doing that it should be doing, or what is it doing that it should not be doing? What error messages are you getting, if any? If you are not getting any errors, is error reporting enabled? You may want to start your script with:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

youthBYTES
11-01-2006, 06:36 PM
it doesn't verify that the record exists:
$result = $DB->query("SELECT * FROM `portfolio` WHERE type='$type' AND title='$title'");
if($row = mysql_fetch_array($result))
{
$exists = 'yes';
}

NogDog
11-01-2006, 08:56 PM
Echo out the values of $type and $title and make sure they're what you think they are.

Check the value of $result. If $result === FALSE then probably there's a syntax error in your query. (Without knowing what the class looks like for your $DB object, I don't know how it handles error reporting.) You might want to try something like this to get more debug info:

$query = "SELECT * FROM `portfolio` WHERE type='$type' AND title='$title'";
$result = mysql_query($query) or die("SQL error: $query - " . mysql_error());
if(mysql_num_rows($result))
{
$exists = 'yes';
}
else // DEBUG, change for production version to "user friendly" version
{
user_error("No match found for query: $query", E_USER_WARNING);
}

youthBYTES
11-03-2006, 12:48 AM
i tried

$result = mysql_query("SELECT title FROM `portfolio` WHERE type='$type' AND title='$title'");
cms_msgBox($result);
exit;


but i get "Resource ID #8" ideas?

NogDog
11-03-2006, 11:14 AM
That means the query was successfully processed. (A resource ID was returned.) It does not necessarily mean anything was matched, though. You could try using mysql_num_rows($result); to find our how many (if anY) matches were found.

youthBYTES
11-03-2006, 10:18 PM
i found my problem, i was using || which i had assumed was AND which is actually OR as i found out, boy, i feel dumb ;x

NogDog
11-03-2006, 11:54 PM
i found my problem, i was using || which i had assumed was AND which is actually OR as i found out, boy, i feel dumb ;x
Note that you can just use the words and and or instead of the && and || operators. (There's a difference in operator precedence, but most of the time it will not matter.)