Program using PDO to receive value if value exist in table data not working
<p>So I am trying to check if my database companynames has a CompanyName in it.  If it does not I want the company name that was sent in to be inserted.  The code I have is not working to well for me.  Let me know what you think</p>

<pre><code>    /***************************************VALIDATE CHECK BOXES ARE CHECKED *********************************************************/
            $setError = 0;
            $ch1 = 'unchecked';
            $ch2 = 'unchecked';
        //  print_r("testing ch1 : $ch1");
            if(isset($_POST['only-thisform']))  {

                $ch1 = 'Check';
                //if ($setChecker != 1)
                $setError= 0;
                //print_r("testing ch1: $ch1");

            }
            if ($ch1 !='Check' ){

            $setError = 1;
            returnBack();

        }
            if (isset($_POST['approval'])) {
                //$setChecker = 1;
                $ch2 = 'Check';
                $setError= 0;
                //print_r("testing  ch2: $ch2");
            }
             if ($ch2 != 'Check' ){

            $setError = 1;
            returnBack();

        }
/***************************************************************************************************************************************/       

function returnBack(){
    header("Location: http://pcgprotects.com/PCG/test.php?seterror=1");
    exit;

}


if(isset($_POST['compname']))  {
    $compname=$_POST['compname'];
}
else{
    $setError = 1;
    returnBack();
}

if(isset($_POST['phone']))  {
    $phone=$_POST['phone'];
}
else{
    $setError = 1;
    returnBack();
}

if(isset($_POST['policynum']))  {
    $policynum=$_POST['policynum'];
}
else{
    $setError = 1;
    returnBack();
}

if(isset($_POST['nameofPolicyholder']))  {
    $nameofPolicyholder=$_POST['nameofPolicyholder'];
}
else{
    $setError = 1;
    returnBack();
}

if(isset($_POST['mailing']))  {
    $mailing=$_POST['mailing'];
}
else{
    $setError = 1;
    returnBack();
}
if(isset($_POST['city']))  {
    $city=$_POST['city'];
}
else{
    $setError = 1;
    returnBack();
}
if(isset($_POST['mailing']))  {
    $mailing=$_POST['mailing'];
}
else{
    $setError = 1;
    returnBack();
}
if(isset($_POST['hiddenfield']))  {
    $state=$_POST['hiddenfield'];
}
else{
    $setError = 1;
    returnBack();
}




/*mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$new_id_number = mysql_insert_id();
$query = "INSERT INTO contacts VALUES ('$new_id_number','$compname','$phone','$policynum','$nameofPolicyholder','$mailing')";
echo 'being inserted';
mysql_query($query);*/ 

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH-&gt;prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND);   
}
/*$compare = "SELECT * FROM companynames WHERE CompanyNames = :compname";
if ($compare != $compname){
    $query = "INSERT INTO companynames VALUES('$new_id_number','$compname') IF NOT EXISTS";
    mysql_query($query);
    echo "working";
    echo "$state";
}
else
    echo "already inserteed";
mysql_close();*/

$sql = "SELECT COUNT(*) FROM companynames WHERE CompanyNames = :compname";

if ($res = $conn-&gt;query($sql)) {

    /* Check the number of rows that match the SELECT statement */
  if ($res-&gt;fetchColumn() &gt; 0) {

        /* Issue the real SELECT statement and work with the results */
         $sql = "SELECT CompanyNames FROM companynames WHERE CompanyNames != :compname";
       foreach ($conn-&gt;query($sql) as $row) {
           print "Name: " .  $row['CompanyNames'] . "\n";
         }
    }
    /* No rows matched -- do something else */
  else {
      print "No rows matched the query.";
    }
}
echo "tsting";
$res = null;
$conn = null;
$DBH = null; 
</code></pre>
