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. Link to comment https://forums.phpfreaks.com/topic/96760-whats-wrong-with-my-code/ 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. Link to comment https://forums.phpfreaks.com/topic/96760-whats-wrong-with-my-code/#findComment-495154 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 Link to comment https://forums.phpfreaks.com/topic/96760-whats-wrong-with-my-code/#findComment-495159 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. Link to comment https://forums.phpfreaks.com/topic/96760-whats-wrong-with-my-code/#findComment-495192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.