Jump to content

PHP SQL ALERT


plznty

Recommended Posts

Duplicate entry ' ' for key 'username'

Comes up when i try and register the same username.

How can I make it say "Sorry this username has already been taken" rather than "Duplicate entry ' ' for key 'username'"

 

Before executing the INSERT query in the table, do a check on the existence of the username (SELECT query) supplied through the form and if does not exist, then do the insert, otherwise, show the error message.

 

Does it help?

 

Link to comment
Share on other sites

Before You do the insert into database do a check if the username already exists, something like:

 

"SELECT table_id FROM users WHERE username = '$your_input_var'"

 

If this statement returns number of rows > 0 then print out "Sorry this username has already been taken"

 

Hope this helps

Link to comment
Share on other sites

you can have a custom error message... but it will show up if any sql errors occur, not just username duplicates.

$ok = @mysql_query("INSERT rara mysql command") or die("This username has been taken");

 

otherwise you'd have to use an sql query to see if that username is taken and then use an if command in the php..

i.e.

$username = $_POST['username'];

$sql = @mysql_query("SELECT username FROM users WHERE username = $username");

 

if (mysql_num_rows($sql)) {

exit("Username has been taken");

} else {

$ok = @mysql_query("INSERT rara mysql command");

}

 

you may want to have something like $errorMessage = "Username has been taken" and call that variable in the page rather than using exit... its pretty ugly

Link to comment
Share on other sites

if ( $_GET['view'] == process )
{
// Gathered Information
$Zuser = $_POST[user];
$pass = $_POST[pass];
$cash = $_POST[cash];
$Zquestion = $_POST[question];
$Zanswer = $_POST[answer];

// Lowercase gathered information
$user = strtolower($Zuser);
$question = strtolower($Zquestion);
$answer = strtolower($Zanswer);

mysql_query("INSERT INTO users 
(id, username, password, money, current, rank, secretquestion, secretanswer) VALUES('', '$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
or die(mysql_error());  

echo "User Created!";

}

Could you help me more please, i keep getting errors.

Thats the registration area.

Link to comment
Share on other sites

if ( $_GET['view'] == process )
{
// Gathered Information
$Zuser = $_POST[user];
$pass = $_POST[pass];
$cash = $_POST[cash];
$Zquestion = $_POST[question];
$Zanswer = $_POST[answer];

// Lowercase gathered information
$user = strtolower($Zuser);
$question = strtolower($Zquestion);
$answer = strtolower($Zanswer);

mysql_query("INSERT INTO users 
(id, username, password, money, current, rank, secretquestion, secretanswer) VALUES('', '$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
or die(mysql_error());  

echo "User Created!";

}

Could you help me more please, i keep getting errors.

Thats the registration area.

 

If "id" is an auto incremented field, remove '' from the VALUES in the SQL. Your SQL will look like:

 

mysql_query("INSERT INTO users 
(username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
or die(mysql_error());  

 

Does this help?

 

Link to comment
Share on other sites

if ( $_GET['view'] == process )
{
// Gathered Information
$Zuser = $_POST[user];
$pass = $_POST[pass];
$cash = $_POST[cash];
$Zquestion = $_POST[question];
$Zanswer = $_POST[answer];

// Lowercase gathered information
$user = strtolower($Zuser);
$question = strtolower($Zquestion);
$answer = strtolower($Zanswer);

mysql_query("INSERT INTO users 
(id, username, password, money, current, rank, secretquestion, secretanswer) VALUES('', '$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
or die(mysql_error());  

echo "User Created!";

}

Could you help me more please, i keep getting errors.

Thats the registration area.

 

If "id" is an auto incremented field, remove '' from the VALUES in the SQL. Your SQL will look like:

 

mysql_query("INSERT INTO users 
(username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
or die(mysql_error());  

 

Does this help?

 

It works fine this part how it is.

I need help in changing the outcome of duplicate users.

Link to comment
Share on other sites

It works fine this part how it is.

I need help in changing the outcome of duplicate users.

 

Try this

 

<?php
if ( $_GET['view'] == process )
{
// Gathered Information
$Zuser = $_POST['user'];
$pass = $_POST['pass'];
$cash = $_POST['cash'];
$Zquestion = $_POST['question'];
$Zanswer = $_POST['answer'];

// Lowercase gathered information
$user = strtolower($Zuser);
$question = strtolower($Zquestion);
$answer = strtolower($Zanswer);

// Check whether $user exists or not
$sql = "SELECT `username` FROM `user` WHERE `username` = '$user'";
$rs = mysql_query($sql) or die(mysql_error());

// If number of rows returned is more than 1, it means username is duplicated
if ( mysql_num_rows($rs) > 1 ) {
echo "Duplicate username";
}
else {
mysql_query("INSERT INTO users 
(username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
or die(mysql_error());  

echo "User Created!";
}

}
?>

 

Link to comment
Share on other sites

It works fine this part how it is.

I need help in changing the outcome of duplicate users.

 

Try this

 

<?php
if ( $_GET['view'] == process )
{
// Gathered Information
$Zuser = $_POST['user'];
$pass = $_POST['pass'];
$cash = $_POST['cash'];
$Zquestion = $_POST['question'];
$Zanswer = $_POST['answer'];

// Lowercase gathered information
$user = strtolower($Zuser);
$question = strtolower($Zquestion);
$answer = strtolower($Zanswer);

// Check whether $user exists or not
$sql = "SELECT `username` FROM `user` WHERE `username` = '$user'";
$rs = mysql_query($sql) or die(mysql_error());

// If number of rows returned is more than 1, it means username is duplicated
if ( mysql_num_rows($rs) > 1 ) {
echo "Duplicate username";
}
else {
mysql_query("INSERT INTO users 
(username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
or die(mysql_error());  

echo "User Created!";
}

}
?>

 

Still says

Duplicate entry '(INPUT TEXT)' for key 'username'

Link to comment
Share on other sites

Any more help?

 

Please provide your HTML / Form part that is posted to the problematic script.

 

<?php
include ("include/config.php");
echo "<font face='verdana' color='#D2D2D2'>
<style type='text/css'>
Body { Background: transparent; }
</style>
<center>";
if ( $_GET['view'] == create )
{
echo "
<form action='register.php?view=process' method='post'>
<table border='0'>
<tr>
<td colspan='2' align='center'> 
<b><font face='verdana' color='#D2D2D2'>Registration</font></b> 
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Username:</font></td>
<td><input type=text name=user size=20 maxlength=12></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Password:</font></td>
<td><input type=password name=pass size=20 maxlength=20></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>GP (millions)</font></td>
<td><input type=text name=cash size=20 maxlength=4></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Secret Question</font></td>
<td><input type=text name=question size=20 maxlength=20></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Secret Answer</font></td>
<td><input type=text name=answer size=20 maxlength=20></td>
</tr>
<tr>
<td colspan='2' align='center'> 
<b><input type=submit value=Proceed></b> 
</tr>
</table>";
}

if ( $_GET['view'] == process )
{
// Gathered Information
$Zuser = $_POST['user'];
$pass = $_POST['pass'];
$cash = $_POST['cash'];
$Zquestion = $_POST['question'];
$Zanswer = $_POST['answer'];

// Lowercase gathered information
$user = strtolower($Zuser);
$question = strtolower($Zquestion);
$answer = strtolower($Zanswer);

// Check whether $user exists or not
$sql = "SELECT `username` FROM `users` WHERE `username` = '$user'";
$rs = mysql_query($sql) or die(mysql_error());

// If number of rows returned is more than 1, it means username is duplicated
if ( mysql_num_rows($rs) > 1 ) {
   echo "Duplicate username";
}
else {
   mysql_query("INSERT INTO users 
   (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
   or die(mysql_error());  
   
   echo "User Created!";
}

}
?>

Link to comment
Share on other sites

Any more help?

 

Please provide your HTML / Form part that is posted to the problematic script.

 

<?php
include ("include/config.php");
echo "<font face='verdana' color='#D2D2D2'>
<style type='text/css'>
Body { Background: transparent; }
</style>
<center>";
if ( $_GET['view'] == create )
{
echo "
<form action='register.php?view=process' method='post'>
<table border='0'>
<tr>
<td colspan='2' align='center'> 
<b><font face='verdana' color='#D2D2D2'>Registration</font></b> 
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Username:</font></td>
<td><input type=text name=user size=20 maxlength=12></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Password:</font></td>
<td><input type=password name=pass size=20 maxlength=20></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>GP (millions)</font></td>
<td><input type=text name=cash size=20 maxlength=4></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Secret Question</font></td>
<td><input type=text name=question size=20 maxlength=20></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Secret Answer</font></td>
<td><input type=text name=answer size=20 maxlength=20></td>
</tr>
<tr>
<td colspan='2' align='center'> 
<b><input type=submit value=Proceed></b> 
</tr>
</table>";
}

if ( $_GET['view'] == process )
{
// Gathered Information
$Zuser = $_POST['user'];
$pass = $_POST['pass'];
$cash = $_POST['cash'];
$Zquestion = $_POST['question'];
$Zanswer = $_POST['answer'];

// Lowercase gathered information
$user = strtolower($Zuser);
$question = strtolower($Zquestion);
$answer = strtolower($Zanswer);

// Check whether $user exists or not
$sql = "SELECT `username` FROM `users` WHERE `username` = '$user'";
$rs = mysql_query($sql) or die(mysql_error());

// If number of rows returned is more than 1, it means username is duplicated
if ( mysql_num_rows($rs) > 1 ) {
   echo "Duplicate username";
}
else {
   mysql_query("INSERT INTO users 
   (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
   or die(mysql_error());  
   
   echo "User Created!";
}

}
?>

 

I have already quoted this.

 

First change this:

if ( $_GET['view'] == process )

 

To:

if ( $_GET['view'] == 'process' )

 

 

Also:

if ( $_GET['view'] == 'create' )

 

All those lines will produce Notice internally, if the error messages are turned off. Always do this while you develop a script:

 

error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('log_errors', '0');

 

in top of your every script.

 

Link to comment
Share on other sites

Any more help?

 

Please provide your HTML / Form part that is posted to the problematic script.

 

<?php
include ("include/config.php");
echo "<font face='verdana' color='#D2D2D2'>
<style type='text/css'>
Body { Background: transparent; }
</style>
<center>";
if ( $_GET['view'] == create )
{
echo "
<form action='register.php?view=process' method='post'>
<table border='0'>
<tr>
<td colspan='2' align='center'> 
<b><font face='verdana' color='#D2D2D2'>Registration</font></b> 
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Username:</font></td>
<td><input type=text name=user size=20 maxlength=12></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Password:</font></td>
<td><input type=password name=pass size=20 maxlength=20></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>GP (millions)</font></td>
<td><input type=text name=cash size=20 maxlength=4></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Secret Question</font></td>
<td><input type=text name=question size=20 maxlength=20></td>
</tr>
<tr>
<td><font face='verdana' color='#D2D2D2'>Secret Answer</font></td>
<td><input type=text name=answer size=20 maxlength=20></td>
</tr>
<tr>
<td colspan='2' align='center'> 
<b><input type=submit value=Proceed></b> 
</tr>
</table>";
}

if ( $_GET['view'] == process )
{
// Gathered Information
$Zuser = $_POST['user'];
$pass = $_POST['pass'];
$cash = $_POST['cash'];
$Zquestion = $_POST['question'];
$Zanswer = $_POST['answer'];

// Lowercase gathered information
$user = strtolower($Zuser);
$question = strtolower($Zquestion);
$answer = strtolower($Zanswer);

// Check whether $user exists or not
$sql = "SELECT `username` FROM `users` WHERE `username` = '$user'";
$rs = mysql_query($sql) or die(mysql_error());

// If number of rows returned is more than 1, it means username is duplicated
if ( mysql_num_rows($rs) > 1 ) {
   echo "Duplicate username";
}
else {
   mysql_query("INSERT INTO users 
   (username, password, money, current, rank, secretquestion, secretanswer) VALUES('$user', '$pass', '$cash', '0', 'noob', '$question','$answer' ) ") 
   or die(mysql_error());  
   
   echo "User Created!";
}

}
?>

 

I have already quoted this.

 

First change this:

if ( $_GET['view'] == process )

 

To:

if ( $_GET['view'] == 'process' )

 

Done.. but it really doesnt help this situation and the problems im having one bit.. ;/

Link to comment
Share on other sites

Also change:

if ( $_GET['view'] == create )

 

To:

if ( $_GET['view'] == 'create' )

 

All those lines will produce Notice internally and bypasses few lines. Always do this while you develop a script:

 

error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('log_errors', '0');

 

in top of your every script.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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