Jump to content

Non existent duplicate error


pcw

Recommended Posts

Hi, I got this script which checks a username does not exist, before writing the users registration info to the database. It was working fine, but now for some reason says there is a duplicate username in the database, when there isnt

 

<?php


$password1 = $_POST['password1'];
$password2 = $_POST['password2'];

if ($password1 != $password2) {

echo "Your passwords did not match, please go back and register again";

} else {

// Check Passwords Match


// Get Posted Variables

$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$town = $_POST['town'];
$county = $_POST['county'];
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$approved = "no";

// MySQL Connection

include_once("data/mysql.php");

$mysqlPassword = (base64_decode($mysqlpword));

$con = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die(mysql_error());
mysql_select_db("$dbname", $con) or die(mysql_error());


$query = "SELECT COUNT(*) AS count FROM members WHERE username='$username'";
$results = mysql_query($query) or die ("Error reading from database");
$existingUsername = mysql_fetch_array($results);

if ($existingUsername['count'] > 0) {
echo "I'm sorry our database already contains that username please choose a new username to continue.";
} else {
    

$sql = "INSERT INTO members (username, firstname, lastname, address, town, county, postcode, email, password, approved)
VALUES
('$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[address]','$_POST[town]','$_POST[county]','$_POST[postcode]','$_POST[email]','$_POST[password2]','$approved')";

mysql_query($sql,$con);

?>

<meta http-equiv="REFRESH" content="5;url=../login.html">

<?php

}

mysql_close($con);
}
?>

 

Im sure it is something simple, I just cant see what it is.

 

As always, any help is much appreciated

Link to comment
https://forums.phpfreaks.com/topic/230738-non-existent-duplicate-error/
Share on other sites

EDIT: What's the exact error message?  I assumed you were referring to this line:

echo "I'm sorry our database already contains that username please choose a new username to continue.";

 

Echo out $query to make sure the username you expect is actually being queried with.  If not, we would need to see your HTML form, maybe the name is different.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.