spanner206 Posted November 21, 2013 Share Posted November 21, 2013 hi just wondering how i would go about checking if a companys name is already in use and if it is show a message at the side saying u cant use this name as its already in use, i know how i would do the error message but i got no clue on how to do the check im guessing you use a sql query but i dont really know. this is the format ive been doing my code in. if (empty($_POST["companyname"])) { $errors['companyname'] = "Please Enter Your companyname"; }elseif(preg_match_all("/[(?\/<>*:.@)]/i",$_POST['companyname'],$matches)){ // list of invalid characters #echo '<pre>',print_r($matches),'</pre>'; $invalid_characters = implode(",",$matches[0]); $errors['companyname'] = 'Cannot use '.$invalid_characters; } elseif(strlen($_POST['companyname']) > 220) { $errors['companyname'] = 'Company name must be less then 220 characters'; } elseif(strpos($_POST['companyname'], 'The') !== false) { $errors['companyname'] = 'Company Names cant start with The'; } elseif(strpos($_POST['companyname'], 'the') !== false) { $errors['companyname'] = 'Company Names cant start with the'; } elseif(strpos($_POST['companyname'], 'THE') !== false) { $errors['companyname'] = 'Company Names cant start with THE'; } else { $companyname = test_input($_POST["companyname"]); } Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2013 Share Posted November 21, 2013 Off topic but why would you want to bar company names like Theakstons or Wetherspoons? Quote Link to comment Share on other sites More sharing options...
spanner206 Posted November 21, 2013 Author Share Posted November 21, 2013 yh ive been trying to sort that out my boss wanted me to block the for some reason but i dont no how i would stop that from happening Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2013 Share Posted November 21, 2013 Your code will flag an error if "the" occurs anywhere in the name. You need to check for "the " (with space) in position 0 $company_names = array ( 'Theakstons', 'The Rams', 'Wetherspoons', 'THE HAMMERS', 'the tangerines' ); foreach ($company_names as $name) { echo "$name : "; if (stripos($name, 'the ')===0) { echo 'FAIL - cannot begin with "The"<br>'; } else { echo 'PASS<br>'; } } /* RESULTS Theakstons : PASS The Rams : FAIL - cannot begin with "The" Wetherspoons : PASS THE HAMMERS : FAIL - cannot begin with "The" the tangerines : FAIL - cannot begin with "The" */ Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2013 Share Posted November 21, 2013 Back to topic - do you have a database table of existing company names? Quote Link to comment Share on other sites More sharing options...
spanner206 Posted November 21, 2013 Author Share Posted November 21, 2013 yh tbl_club_contacts Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2013 Share Posted November 21, 2013 Assuming that table has a column 'company_name' $sql = sprintf("SELECT COUNT(*) as total FROM tbl_club_contacts WHERE company_name = '%s' ", $db->real_escape_string($companyname)); $result = $db->query($sql); $row = $result->fetch_assoc(); if ($row['total'] > 0) { $errors['companyname'] = "The name '$companyname' already exists."; } Quote Link to comment Share on other sites More sharing options...
spanner206 Posted November 22, 2013 Author Share Posted November 22, 2013 hi ive tried both sets of code, the sql code is showing an error and a notice ( ! ) Notice: Undefined variable: db in C:\wamp\www\AddLeads\addeadstemplate.php on line 55 Call Stack # Time Memory Function Location 1 0.0000 189704 {main}( ) ..\addeadstemplate.php:0 ( ! ) Fatal error: Call to a member function real_escape_string() on a non-object in C:\wamp\www\AddLeads\addeadstemplate.php on line 55 Call Stack # Time Memory Function Location 1 0.0000 189704 {main}( ) ..\addeadstemplate.php:0 and the code for 'the' is just showing this once i press submit, Theakstons : PASS The Rams : FAIL - cannot begin with "The"Wetherspoons : PASSTHE HAMMERS : FAIL - cannot begin with "The" knowing me i put the code in the wrong place heres what it currently looks like. the tangerines : FAIL - cannot begin with "The" <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php $con = mysqli_connect("localhost","root","","nib"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // define variables and set to empty values $companyname = $firstname = $address1 = $address2 = $county = $city = $postcode = $email = $website = $clubphone = $homephone = $mobilephone = $typeofbusiness = $renewaldate = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $errors = array(); if (empty($_POST["companyname"])) { $errors['companyname'] = "Please Enter Your companyname"; }elseif(preg_match_all("/[(?\/<>*:.@)]/i",$_POST['companyname'],$matches)){ // list of invalid characters #echo '<pre>',print_r($matches),'</pre>'; $invalid_characters = implode(",",$matches[0]); $errors['companyname'] = 'Cannot use '.$invalid_characters; } $company_names = array ( 'Theakstons', 'The Rams', 'Wetherspoons', 'THE HAMMERS', 'the tangerines' ); foreach ($company_names as $name) { echo "$name : "; if (stripos($name, 'the ')===0) { echo 'FAIL - cannot begin with "The"<br>'; } else { echo 'PASS<br>'; } } $sql = sprintf("SELECT COUNT(*) as total FROM tbl_club_contacts WHERE companyname = '%s' ", $db->real_escape_string($companyname)); $result = $db->query($sql); $row = $result->fetch_assoc(); if ($row['total'] > 0) { $errors['companyname'] = "The name '$companyname' already exists."; } else { $companyname = test_input($_POST["companyname"]); } Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 22, 2013 Share Posted November 22, 2013 (edited) Change $db to $con on line 55. Also the code Barand gave you here was an example for checking the word 'the' was at the start of the string. You need to adapt it to check the value in $_POST["companyname"]. It is not meant for a simple copy and paste solution. Edited November 22, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
spanner206 Posted November 22, 2013 Author Share Posted November 22, 2013 yh that got rid of the error but if i type in the same company name twice its still going onto the database twice. any ideas??? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 22, 2013 Share Posted November 22, 2013 Move this code $sql = sprintf("SELECT COUNT(*) as total FROM tbl_club_contacts WHERE companyname = '%s' ", $db->real_escape_string($companyname)); $result = $db->query($sql); $row = $result->fetch_assoc(); if ($row['total'] > 0) { $errors['companyname'] = "The name '$companyname' already exists."; } So it is after this else { $companyname = test_input($_POST["companyname"]); } Quote Link to comment Share on other sites More sharing options...
spanner206 Posted November 22, 2013 Author Share Posted November 22, 2013 waaaaay you only gone and done it again cheers ch0cu3r now its just this the, The, THE code Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted November 22, 2013 Solution Share Posted November 22, 2013 ...now its just this the, The, THE code Look at my example, engage brain, see what it is doing and adapt your code to do it the same way Quote Link to comment Share on other sites More sharing options...
spanner206 Posted November 22, 2013 Author Share Posted November 22, 2013 cheers for everyones help couldnt of done it without you 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.