Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I check if username already exists in database?

I need to check if username is already taken in database, can someone help me?

Here's my full code:

<?php include ('db_connect.php');

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona...

<html xmlns="http://www.w3.org/1999/xhtml%22%3E

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

function validation() {

var invalid = " "; // Invalid character is a space

var minLength = 6; // Minimum length password

var minLength2 = 4; //minimum length username

var pw1 = document.create.password.value;

var pw2 = document.create.password2.value;

var name = document.create.name.value;

// check for a value in both fields.

if (pw1 == '' || pw2 == '') {

alert('Please enter your password twice.');

return false;

}

//check for value in name

if (document.create.name.value == "") {

alert ("Username required");

return false;

}

// check for minimum length password

if (document.create.password.value.length < minLength) {

alert('Your password must be at least ' + minLength + ' characters long. Try again.');

return false;

}

// check for minimum length Username

if (document.create.name.value.length < 4) {

alert('Your username must be at least 4 characters long. Try again.');

return false;

}

// check for spaces password

if (document.create.password.value.indexOf(invalid) > -1) {

alert("Your password may not contain any spaces.");

return false;

}

// check for spaces username

if (document.create.name.value.indexOf(invalid) > -1) {

alert("Your username may not contain any spaces.");

return false;

}

else {

if (pw1 != pw2) {

alert ("You did not enter the same password twice. Please re-enter your password.");

return false;

}

else {

return true;

}

}

}

// End -->

</script>

</HEAD>

</head>

<body>

<p>

<?php

if (isset ($_POST['Submit' ]))

{

$name = $_POST['name' ];

$password = $_POST['password' ];

$query = "INSERT INTO accounts (name, password) VALUES('$name', '$password')" ;

mysql_query ($query );//kører forespørgsel

mysql_close ();

header ("location:confirm.php" );

}

?>

<br />

</p><center><h1>Create account</h1></center><hr/>

<center>

<form id= "create" name="create" method= "post" onsubmit="return validation()" action="" >

<table width= "310" border= "0" cellspacing= "" cellpadding= "0">

<tr>

<td><b>Username:</b></td>

<td><input type= "text" name="name" id="name" maxlength=12/></td>

<tr><td><div class="validation">Min 4 - Max 12 characters. No special characters. No spaces.</div></td></tr>

</tr>

<tr>

<td><b>Password:</b></td>

<td><label>

<input type="password" name="password" id="password" maxlength=12/>

<tr><td><div class="validation">Min 6 - Max 12 characters. No special characters. No spaces.</div></td></tr>

</label></td>

</tr>

<tr>

<td><b>Repeat password:</b></td>

<td><label>

<input type="password" name="password2" id="password2" maxlength=12/>

</label></td>

</tr>

<tr>

<td> </td>

<td><input type="submit" name="Submit" id="Submit" value="Create account" /></td>

</tr>

</table>

<hr/>

</form>

</center>

</script>

</body>

</html>

1 Answer

Relevance
  • Anonymous
    9 years ago
    Favorite Answer

    Before inserting data make a SELECT query like:

    "SELECT * FROM accounts WHERE NAME='$name'"

    if this query will return an empty record set then this name doesn't exist.

Still have questions? Get your answers by asking now.