FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


MySQL Database...unique usernames





guitarcrazy087
Ok, all I'm looking to do is have users fill out a simple form and then have PHP write the data to a database. I'm trying to get a couple of other variables worked into this too, one of which is that if the entered username already exists, it brings up an error.

Here's the code for the form:

Code:
<form method="post" action="makeaccount.php">

Username: <input type="text" name="username" /><br />

Password: <input type="password" name="password" /><br />

Validate Password: <input type="password" name="passwordcheck" /><br />

<input type="submit" value="Sign up!" />

</form>


And here's the code for makeaccount.php:

Code:
<?php

$UserName  = $_POST['username'];
$PassWord  = $_POST['password'];
$PassCheck = $_POST['passwordcheck'];

$Connection = mysql_connect("localhost","username","password");
if (!$Connection) {
  die ('Could not connect to database...');
  }
mysql_select_db("guitarcr_mysite", $Connection);

$Get   = "SELECT Username FROM Users WHERE Username=$UserName";
$Query = mysql_query($Get,$Connection);
$Rows  = mysql_affected_rows($Query);

mysql_close($Connection);

if (strlen($UserName)>30  || empty($UserName)) {
  echo 'Your username is either too long or you have not filled out the field.  Your username cannot be more that 30 characters.';
  }
else if ($Rows < 0) {
  echo 'Username is already taken.';
  }
else if (strlen($PassWord)>30 || empty($PassWord)) {
  echo 'Your password is either too long or you have not filled out the field.  Your password cannot be more than 30 characters';
  }
else if ($PassWord !== $PassCheck) {
  echo 'The password fields do not match.  Please enter your password again.';
  }
else {
  $Connection = mysql_connect("localhost","username","password");
  if (!$Connection) {
    die ('Could not connect to database...');
    }
  mysql_select_db("guitarcr_mysite", $Connection);

  mysql_query("INSERT INTO Users (Username, Password)
  VALUES ($UserName, $PassWord)");

  mysql_close($Connection);
  }

?>


I already have one user in the database, and whenever I try to enter that user into the registration form, or a new user, it gives me this error:

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/guitarcr/public_html/makeaccount.php on line 21

Anyway, it all works except for that part (the mysql_affected_rows() part). So i'm wondering what I need to do to get it working...I tried using echo to tell me how many affected rows there were and it only gave me -1.

Any help would be appreciated.

Joe
sonam
mysql_affected_rows() you cannot use if is not before inserted, updated... your DB.

Quote:
Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier .

Sonam
guitarcrazy087
Hey, thanks for your reply....but what do I use just to see if there is already a username entered that matches the username the user entered to register?

Thanks.
kv
There is an error in your query

Code:

$Get   = "SELECT Username FROM Users WHERE Username=$UserName";


Corrected version

Code:

$Get   = "SELECT Username FROM Users WHERE Username='$UserName'"; //Observe quotes around $UserName



Actually mysql_query will have failed because of this. and hence mysql_affected_rows fails too.
guitarcrazy087
Ohhh...ok, I thought that if I had the quotes around the $UserName that the value would be $UserName, not the value of the variable $UserName.

Thanks, I'll try it out Smile
imagefree
i think the code should still not work. Here is the piece of code

Code:
.
.
.
else if ($Rows < 0) {
  echo 'Username is already taken.';
  }
.
.
.


the corrected one is
Code:
else if ($Rows > 0) {
  echo 'Username is already taken.';
  }

If you continue to use the same code as you posted, you will see the error every time anyone tries to signup.

Why are you twice trying to connect to database? use this code:

Code:
.
.
.

else if (strlen($PassWord)>30 || empty($PassWord)) {
  echo 'Your password is either too long or you have not filled out the field.  Your password cannot be more than 30 characters';
  }
else if ($PassWord !== $PassCheck) {
  echo 'The password fields do not match.  Please enter your password again.';
  }
else {

  mysql_query("INSERT INTO Users (Username, Password)  VALUES ('$UserName', '$PassWord')");
//You again missed the single quotes.
}

mysql_close($Connection);
?>
Star Wars Fanatic
guitarcrazy087 wrote:
Hey, thanks for your reply....but what do I use just to see if there is already a username entered that matches the username the user entered to register?

Thanks.


You could make the username column unique, and anytime you try to enter a username that is already taken an error would be returned, and you could check for that.

Also, if you want to see how many rows have been returned, use this function:

int mysql_num_rows ( resource $result )
Related topics
Cron Job to Backup MySQL Database
Mysql database problem
PHPBB MYSQL Database Question.
MySQL database for a forum
Mysql database size?
Connect to MySql database
[MySQL database] Password and username
insert data to mySQL database problem
MySQL Database!?
Delete data from MySQL database problem!!
MySQL database
Problem connecting to MySQL database with PHP [RESOLVED]
Need Help with mysql database of DragonflyCMS
Help importing CSV file into MySQL database
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.