• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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 />';
      }
0
evibesmusic
Asked:
evibesmusic
  • 2
2 Solutions
 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now