Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-15-2013, 11:14 PM   PM User | #1
andynov123
Regular Coder

 
Join Date: Oct 2010
Posts: 246
Thanks: 8
Thanked 1 Time in 1 Post
andynov123 is an unknown quantity at this point
Can’t connect to database

Hi
I'm trying to connect my database for the first time to a registration form but when I fill out the form I am getting this error

Access denied for user 'uj'@'localhost' (using password: YES)

any help is appreciated.
register.php
Code:
<html>
 <body>
  <form action="verify.php" method="post" name="register">
    Username: <input type="text" name="username" />
    Password: <input type="text" name="password" />
    Email: <input type="text" name="email" />
	<input type="hidden" name="form_submitted" value="1"/> 
  <input type="submit" />
  </form>
 </code>
</html>
verify.php
Code:
<?php

mysql_connect("localhost", uj, "myPassword") or die(mysql_error());

mysql_select_db("users") or die(mysql_error());

if ($_POST['form_submitted'] == '1') {
##User is registering, insert data until we can activate it

$activationKey =  mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
$username = mysql_real_escape_string($_POST[username]);
$password = mysql_real_escape_string($_POST[password]);

$email = mysql_real_escape_string($_POST[email]);

$sql="INSERT INTO users (username, password, email, activationkey, status) VALUES ('$username', '$password', '$email', '$activationKey', 'verify')";

if (!mysql_query($sql))

  {

  die('Error: ' . mysql_error());

  }

echo "An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration.";

##Send activation Email

$to      = $_POST[email];

$subject = " YOURWEBSITE.com Registration";

$message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link:\rhttp://www.YOURWEBSITE.com/verify.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ YOURWEBSITE.com Team";

$headers = 'From: noreply@ YOURWEBSITE.com' . "\r\n" .

    'Reply-To: noreply@ YOURWEBSITE.com' . "\r\n" .

    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

} else {

##User isn't registering, check verify code and change activation code to null, status to activated on success

$queryString = $_SERVER['QUERY_STRING'];

$query = "SELECT * FROM users"; 

$result = mysql_query($query) or die(mysql_error());

  while($row = mysql_fetch_array($result)){

    if ($queryString == $row["activationkey"]){

       echo "Congratulations!" . $row["username"] . " is now the proud new owner of an YOURWEBSITE.com account.";

       $sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";

       if (!mysql_query($sql))

  {

        die('Error: ' . mysql_error());

  }

    }

  }

}
?>

Last edited by andynov123; 02-16-2013 at 12:46 AM..
andynov123 is offline   Reply With Quote
Old 02-15-2013, 11:20 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is a sql error: Access denied for user 'uj'@'localhost' (using password: YES). It has rejected your user 'uj' at 'localhost'. It will not tell you if you're username, hostname or password is invalid, so these are what you have to verify.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-15-2013, 11:32 PM   PM User | #3
andynov123
Regular Coder

 
Join Date: Oct 2010
Posts: 246
Thanks: 8
Thanked 1 Time in 1 Post
andynov123 is an unknown quantity at this point
I understand what you are saying, but I don't know what I did wrong?
Check image out for more info on my setup
Attached Thumbnails
Click image for larger version

Name:	img1.jpg
Views:	10
Size:	49.0 KB
ID:	11935  
andynov123 is offline   Reply With Quote
Old 02-16-2013, 04:02 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Nothing; this hasn't a thing to do with your structure. It is your mysql user account that has been rejected and it doesn't verify that the username @ localhost with the password provided as valid.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-16-2013, 05:04 AM   PM User | #5
PappaJohn
Senior Coder

 
Join Date: Apr 2007
Location: Quakertown PA USA
Posts: 1,028
Thanks: 1
Thanked 125 Times in 123 Posts
PappaJohn will become famous soon enough
Incorrect:
Code:
mysql_connect("localhost", uj, "myPassword") or die(mysql_error());
Presumably you mean:
Code:
mysql_connect("localhost", "uj", "myPassword") or die(mysql_error());
__________________
John
PappaJohn is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:00 AM.


Advertisement
Log in to turn off these ads.