2

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?

kenny
  • 438
  • 4
  • 14

2 Answers2

8

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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

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);
?>
Suyog
  • 2,472
  • 1
  • 14
  • 27
  • whats wrong with original query `"SELECT * FROM list WHERE name='$name'"` – arif_suhail_123 Nov 05 '15 at 04:27
  • @arif_suhail_123 there is nothing wrong in `"SELECT * FROM list WHERE name='$name'"` but using `LIKE` and `LOWER` will give more precise result. – Suyog Nov 05 '15 at 04:28
  • 1
    It can be considered as a suggestion rather than a fix.. :-) – Suyog Nov 05 '15 at 04:29
  • Hey, thank you so much for the help. I tried the code but it said that every name I entered already exists. – kenny Nov 05 '15 at 04:31
  • 3
    Using `LIKE` will not be beneficial in this situation. If **John** already exists in the DB, but **Johnny** does not... using `LIKE` will tell me that *Johnny* already exists; when, in fact, it does not. The OP is looking for an **exact** match! – Kuya Nov 05 '15 at 04:33