Jump to content

check if name exists


spanner206
Go to solution Solved by Barand,

Recommended Posts

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"]);
}
Link to comment
Share on other sites

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"
*/
Link to comment
Share on other sites

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.";
}
Link to comment
Share on other sites

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 : PASS
THE 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"]);
}
Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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"]);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.