Is my query syntax correct to check to see if record exists in the db?

Experts,

I can't get the value of this query to equal true (in this case find one matching row), and wonder if it has to do with my SQL syntax?

I'm using this code to process the information passed from my form.  I am trying to double check to make sure that the entry does not exist in the database before placing the new info into the db. I can't get this code to find a duplicate entry even though I know one exists.

Thoughts?

// COLLECT THE INFORMATION POSTED IN THE FORM AND SAVE IN MEMORY
$Month = mysql_real_escape_string($_POST['Month']);
$MonthlyReport = mysql_real_escape_string($_POST['MonthlyReport']);
$Year = mysql_real_escape_string($_POST['Year']);
$Department = mysql_real_escape_string($_POST['Department']);
$Range = mysql_real_escape_string($_POST['Range']);
$Access = mysql_real_escape_string($_POST['Access']);
$Date = mysql_real_escape_string($_POST['Date']);
$Checked = mysql_real_escape_string($_POST['Checked']);
$Site = mysql_real_escape_string($_POST['Site']);
$File = $_FILES['File'];
$type = $_FILES['File']['type'];
$size = $_FILES['File']['size'];

// DEFINE THE FILE NAME AS '$file_part'
$file_part = $_FILES['File']['name'];

// INITIALIZE THE NUMBER OF ERRORS TO ZERO
$errors=0;

// MAKE SURE THAT THE NUID DOES NOT ALREADY EXIST IN THE DATABASE
$res=mysql_query("SELECT MonthlyReport, Month, Year, Range, Department FROM permmed_reports WHERE
 MonthlyReport='$MonthlyReport' AND Month='$Month' AND Year=$Year AND Range='$Range' AND Department='$Department'")
or die(mysql_error());

// BEGIN ERROR MESSAGE FORMATTING
$error="<div style='margin-top:10px;'>
<b>ERROR REPORT</b>
<br /><br />
</div><ol>
";

// IF THE FOLLOWING FIELDS ARE LEFT EMPTY SHOW ERROR MESSAGE
if(!$file_part == ''){
      // IF AN ACCOUNT ALREADY EXISTS FOR THIS NUID THEN SHOW THE ERROR MESSAGE
      if(mysql_num_rows($res) != 0 ){
            $errors=1;
            $error.="
            <li><div class='controllers_message'>
            There is already a record in the database for this entry.
            <br /><br />
            Please click here to review record # <a href='edit_report.php?cmd=edit&id=".$results['id']."'>".$results['id']."</a>.
            </div></li>
            <br/><br/>";
            }
      }
      //IF FILE IS EMPTY THEN SHOW ERROR MESSAGE
      elseif($file_part == ''){
            $errors=1;
            $error.="<li>
            <div class='controllers_message'>You did not choose a file to upload.</div>
            </li>
            <br/><br/>";
            }

// IF ANY ERROR OCCURED SHOW THE ERROR MESSAGES ABOVE THAT APPLY
if($errors==1) {
      echo $error;
      echo '</ol>';
      echo 'Please click <a href="add_report.php">here</a> to try again.';
      echo '<br /><br />';
      }
evibesmusicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bryan ButlerCommented:
Can you get your exact SQL output with debug or output info?  Then run that from an sql prompt to see if it works.  I'm betting it's the date comparisons you are making.  You must have the exact same form/type of variable for month and year for the query to work.  

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bryan ButlerCommented:
If you can output your query to see exactly what it's using to compare, and then query the DB to see what it has in those fields, you will see if they are the same.
0
evibesmusicAuthor Commented:
Thanks for the reality check.  The way my form was passing the information is not what I was asking it to find.  Some easy points for you.  It now works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.