Thursday, April 19, 2012

Using SQL Union to examine reference for Delete


 <?php
 public function deletef()
 {
 # check for post variables */
   if (!isset($_POST["cust_id"])) die("{success: false}");
   try {
     $fID = $_POST["cust_id"];

   # delete */
     # check referential links
     $reference="";
     $checkRefSQL = "SELECT * FROM (
       SELECT COUNT(*) AS count, 'x' as tabl FROM x WHERE cust_id = $fD
       UNION
       SELECT COUNT(*) AS count, 'd' as tabl  FROM d WHERE cust_id = $fID
       UNION
       SELECT COUNT(*) AS count, 's' FROM s  WHERE cust_id = $fID
       UNION
       SELECT COUNT(*) AS count, 'w' FROM w  WHERE cust_id = $fID
       )";

     $rs=$this->db->query($checkRefSQL);
     while ($_row = $this->db->fetch($_rs)){
      if($_row["count"]>0) {
       $reference.=":" . $_row["tabl"]
      }
     }
     if($reference=="") {
     $delCustSQL = "DELETE FROM master WHERE cust_id = " . $_POST["cust_id"];
     $this->db->query($delCustSQL);
     die("{success: true}");
     } else { 
       die("{success: false, message: 'reference exists in tabl' . $reference}");
     }
   # check for errors and report */
   } catch (PDOException $_e){
     mail('someone@example.com','Customer Delete',$delCustSQL);
     die("{success: false}");
   } catch (Exception $_e){
     die("{success: false}");
   }
 }
 ?>

--
Bill Creswell



--
Bill Creswell
Web Development, IT Support, Captioning
http://billcreswell.com
http://www.linkedin.com/in/billcreswell

No comments: