kenny
kenny

Reputation: 444

I can't Seem To Prevent Duplicate Name inputs

Okay, so I've been going at it for a while now. What I'm trying to achieve is to prevent people from being able to create the same name that is entered in the name='name' field. Here's the html code.

<div class="fieldclass"><form action='/newlist.php' method='POST'  id="formID">
Name Your Card <input class='ha' type='text' name='name'><p>
<input type='submit' value='create'/>
        </form>

and this is my mysql page.

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "christmas";

// Create connection
$dbhandle = mysqli_connect ($servername, $username, $password, $dbname) or die ("could not connect to database");


$selected = mysql_connect('christmas', $dbhandle);



$query = mysql_query("SELECT * FROM list WHERE name='$name'");
if(mysql_num_rows($query) > 0){
    echo 'that name already exists';
}else{
    mysql_query("INSERT INTO list(name, one , two, three, four, five, six, seven, eight) VALUES ('$name' , '$one' , '$two' , '$three' , '$four' , '$five' , '$six', '$seven' , '$eight')");

}


mysql_close();
?>

what the heck am I doing wrong?

Upvotes: 2

Views: 100

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74219

There is quite a few things wrong here.

You're using a MySQL keyword list as a function, and that alone will cause you problems.

Sidenote: Yes, I know it's not a "reserved" word, it's a "keyword" and MySQL treats keywords specially if used as a function, which is what you're doing now in your second query, and is the way that MySQL is interpreting it as; a function rather than a table declaration name.

I suggest you rename that table to lists, or wrap it in ticks.

You're also mixing MySQL APIs/functions that do not intermix.

So your new code would read as,
while dropping $selected = mysql_connect('christmas', $dbhandle);

$query = mysqli_query($dbhandle, "SELECT * FROM `list` WHERE name='$name'") 
        or die (mysqli_error($dbhandle));

if(mysqli_num_rows($query) > 0){
    echo 'that name already exists';
}else{
    mysqli_query($dbhandle, "INSERT INTO `list` (name, one, two, three, four, five, six, seven, eight) 
                VALUES ('$name' , '$one' , '$two' , '$three' , '$four' , '$five' , '$six', '$seven' , '$eight')") 
                 or die (mysqli_error($dbhandle));

}


mysqli_close($dbhandle);

Your code is also open to an SQL injection. Use a prepared statement.

Now, we have no idea as to where all your variables are coming from.

Use error reporting.


I also suggest you use a conditional empty() for your variables/inputs.

Otherwise, you may also get other errors that MySQL may complain about.

I.e.:

if(!empty($var)){...}

Ultimately and to make sure there are no duplicate entries in your database, you can set a UNIQUE constraint.

Upvotes: 8

Suyog
Suyog

Reputation: 2482

You are mixing mysql and mysqli.

Try following code.

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "christmas";

// Create connection
$dbhandle = mysqli_connect ($servername, $username, $password, $dbname) or die ("could not connect to database");

$query = mysqli_query($dbhandle, "SELECT * FROM list WHERE LOWER(name) = LOWER('$name') ");
if(mysqli_num_rows($query) > 0){
    echo 'that name already exists';
}else{
    mysqli_query($dbhandle, "INSERT INTO list(name, one , two, three, four, five, six, seven, eight) VALUES ('$name' , '$one' , '$two' , '$three' , '$four' , '$five' , '$six', '$seven' , '$eight')");

}

mysqli_close($dbhandle);
?>

Upvotes: 1

Related Questions