ivytony Posted March 18, 2008 Share Posted March 18, 2008 The code I wrote is to save a domain name in the database. Before storing the domain, the code will check if it already existed. On the HTML page, there's a checkbox that allows duplicate domains, otherwise only one unique domain is allowed in the database. Firstly, I created this function: <?php function duplicates($domain) { //check if the merchant already exists by looking at domain global $db; $n = $db->get_var("SELECT count(*) FROM domains WHERE domain = '$domain'"); //get_var is a function return $n; } ?> Below is to check the domain and store it: <?php $isduplicate = $_POST["duplicatedomain"]; // get the value of checkbox that allows duplicate domain if(duplicates($domain) > 0){ //if domain exists already if($isduplicate == 1){ //allows duplicate domain add_domain(); //a function to store the domain header("Location: admin_domains.php"); } else{ echo 'this domain already exists'; exit; } } else{ add_domain(); header("Location: admin_domains.php"); } ?> My problem is: no matter if the domain existed in database or not, it always stores the domain. I cannot figure out what's wrong. Quote Link to comment Share on other sites More sharing options...
bpops Posted March 18, 2008 Share Posted March 18, 2008 First of all, you're missing a closing bracket on your if(){} statement in the first code. Quote Link to comment Share on other sites More sharing options...
ivytony Posted March 18, 2008 Author Share Posted March 18, 2008 thanks, but there should be no if statement in the first code. I corrected it. sorry Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 18, 2008 Share Posted March 18, 2008 i don't know what $db is, presumably a data transfer object, so I can't tell what you get from $db->get_var("SELECT count(*) FROM domains WHERE domain = '$domain'"); you may not be getting an actual record or value, but instead a record set handle. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.