Duplicate key.. but not duplicate!

General discussions related to php

Moderators: egami, macek, gesf

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

Fri Mar 03, 2017 7:36 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 :/

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:
<?php
session_start();
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')";
mysql_select_db($dbname);
$value = mysql_query($sql,$con);
if(!$value)
{
die('Could not enter data: ' . mysql_error());
}
echo "User succesfully registered";
mysql_close($con);


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

Sat Mar 04, 2017 2:10 am

Please don't post twice, it make it difficult to have one conversation about what is going on and could end up having several solutions which contradict each other.
sdk33
New php-forum User
New php-forum User
Posts: 1
Joined: Sun Mar 12, 2017 5:15 pm
Contact:

Sun Mar 12, 2017 5:55 pm

Did't check Your database to see how the script working?

Its not working because you have $_Post, not $_POST on Your code at this line:
Uname = $_Post['UserName'];
Therefore you do not enter anything in the database, at field Uname, so Uname is empty.

This is why when you try to register a different user with a different username, it says is a duplicate key entry, because you try to add another empty value, on primary key which must be unique.
Post Reply