Inserting to database, says duplicate!

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 14, 2017 2:44 pm

Fri Mar 03, 2017 10:09 pm

I am making a simple registration.php, but the issue I'm having is that:

I can register a user first time, but when I try to register a different user with a different username, it says there is a duplicate key entry...

I can't for the life of me find the issue, I tried looking everywhere and one suggestion was to delete the table and make a new one, which I've done several times :/

The primary key for my table is the username, so it shouldn't matter if the first name or password is different but regardless of what information I put for the second user, it keeps saying duplicate!

Here is the schema of my user table:
Ufname varchar not null,
Uname varchar not null,
Pwd varchar not null,
primary key (uname));

Pretty simple right?

My registration.php:
if (isset($_POST['FullName']) && isset($_POST['UserName']) && isset($_POST['Password']))
$Fname = $_POST['FullName'];
$Uname = $_Post['UserName'];
$Pwd = $_POST['Password'];

echo "Establishing connection to database...<br>";
//servername details are not shown
$con = mysql_connect($servername, $username, $password) or die ("Unable to connect to Mysql");
echo "Connected to MySQL<br>";

$sql = "INSERT INTO user (Fname,Uname,Pwd) VALUES ('$Fname','$Uname','$Pwd')";
$value = mysql_query($sql,$con);
die('Could not enter data: ' . mysql_error());
echo "User succesfully registered";

echo "All fields must be entered!";
php-forum GURU
php-forum GURU
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Sat Mar 04, 2017 2:08 am

Is that field being populated on the database? Just that the line..

Code: Select all

$Uname = $_Post['UserName'];
should probably be

Code: Select all

$Uname = $_POST['UserName'];
Post Reply