12 Replies - 7633 Views - Last Post: 18 February 2011 - 12:45 AM Rate Topic: -----

#1 Guest_Guest*


Reputation:

Warning: mysql_connect() [function.mysql-connect]: error.

Posted 16 February 2011 - 04:45 PM

Hi, I'm working on creating a register/login form for a website I keep getting following errors I'm not sure how to fix them.
Underneath these errors are my code.

Notice: Undefined index: submit in C:\wamp\www\register.php on line 5
Notice: Undefined index: fullname in C:\wamp\www\register.php on line 7
Notice: Undefined index: username in C:\wamp\www\register.php on line 8
Notice: Undefined index: password in C:\wamp\www\register.php on line 9
Notice: Undefined index: repeatpassword in C:\wamp\www\register.php on line 10

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in C:\wamp\www\register.php on line 17
Couldn't connect


Register.php
<?php 

echo "<h1>Register</h1>";

$submit = $_POST['submit'];

$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = date("Y-m-d");

if ($submit);
{

// open database
			$connect = mysql_connect("localhost", "root", "password") or die("Couldn't connect!");
mysql_select_db("test") or die("Couldn't find database");

  if ($fullname&&$username&&$password&&$repeatpassword)
  {
	
	if ($password==$repeatpassword)
	{
		 
		 if (strlen($fullname)>25||strlen($username)>25)
	   {
	    echo "Length of username of fullname is to long!";
	   }
	   else 
	     {
		 
		 // check password length
		 if (strlen($password)>25||strlen($password)<6)
		   {
		    echo "Password must be between 6 and 25 characters";
		   }
		   else
		    {
			// register user!
			
			  // Check if username is taken
			  $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");
  $count = mysql_num_rows($namecheck);
  
  if ($count!=0)
  {
    die("Username already taken! <a href='register.php'>Click here to try again!</a>");
  }
			
			// encrypt password
	$password =  md5 ($password);
	$repeatpassword = md5 ($repeatpassword);
	

    $queryreg = mysql_query("INSERT INTO users VALUES ('$id','$fullname','$username','$password','$date')");
	
	die("You have been registered! <a href='index.php'>Return</a> to login page");
			
			}	
		 
		 }
		 
	  }	
	  else
	   echo "Your passwords do not match"; 
	
	}
	else
	  echo "Please fill in all fields";
	 
}

?>


<html>

<form action="register.php" method='POST'>
  <table>
<tr>
	 <td>
	  Your Full Name
	  </td>
	  <td>
	  <input type='text' name='fullname' value="<?php echo $fullname; ?>" >
	  </td>
	  </tr>  
<tr>
	 <td>
	  Choose a username
	  </td>
	  <td>
	  <input type='text' name='username' value="<?php echo $username; ?>" >
	  </td>
	  </tr>
<tr>
	 <td>
	  Choose a password
	  </td>
	  <td>
	  <input type='password' name='password' >
	  </td>
	  </tr>  
 <tr>
	 <td>
	  Repeat password
	  </td>
	  <td>
	  <input type='password' name='repeatpassword' >
	  </td>
	  </tr>
	</table>
	
	<p><input type='submit' name='submit' value='Register' ></p>

</form>

</html>


Index.php
<html>
	<form action='login.php' method='POST'>
		Username: <input type='text' name='username'><br>
		Password: <input type='password' name='password'><br>
		<input type='submit' value='Log in'>
	</form>
	<a href ='register.php'>Register?</a>
</html>


MOD EDIT: Moved code into post. Don't link offsite for code, copy and paste your code into CODE tags:

:code:

This post has been edited by JackOfAllTrades: 17 February 2011 - 04:58 AM


Is This A Good Question/Topic? 0

Replies To: Warning: mysql_connect() [function.mysql-connect]: error.

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 16 February 2011 - 04:48 PM

The credentials you're passing to mysql_connect() are incorrect.

The undefined index errors are because you're using parts of an array (looks like stuff out of $_POST) when they're not declared. You should use isset() before trying to use data from those variables.

All of this stuff is pretty standard. I didn't even have to look at the code to know what was wrong. A simple Google search would have turned up these results for you immediately.

This post has been edited by Valek: 16 February 2011 - 04:53 PM

Was This Post Helpful? 2
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 16 February 2011 - 05:22 PM

I changed to isset but I'm still getting the same errors
Was This Post Helpful? 0

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 17 February 2011 - 05:00 AM

Because you're not POSTing data to register.php! That looks like you copy/pasted code from login.php and just expected it to work with a few changes.

Damn, I missed that. Thanks, Valek!

This post has been edited by JackOfAllTrades: 17 February 2011 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 17 February 2011 - 01:51 PM

Actually, he is POSTing to register.php with his registration form. His index only contains a link to that registration form, and a form for logging in.

Only time I've ever seen that access denied message in any language, not just PHP, is when the credentials for connecting to the database are incorrect.
Was This Post Helpful? 2
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 17 February 2011 - 02:18 PM

You haven't reached the point of this being an issue yet, but where is the value of $id coming from here?

$queryreg = mysql_query("INSERT INTO users VALUES ('$id','$fullname','$username','$password','$date')");

Was This Post Helpful? 2
  • +
  • -

#7 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 17 February 2011 - 02:25 PM

Like JackOfAllTrades said, I dont see $id being set.

1. $id should be removed IF your ID Column in the Database is AUTO INCREMENT

2. Your SQL Query needs the column names then the values.

With $id set, (non-auto-incrementing id column)
$queryreg = mysql_query("INSERT INTO users (IDCol, FullNameCol, UserNameCol, PassWordCol, DateRegCol) VALUES ('$id','$fullname','$username','$password','$date')");


Without $id set, (auto-incrementing id column)
$queryreg = mysql_query("INSERT INTO users (FullNameCol, UserNameCol, PassWordCol, DateRegCol) VALUES ('$fullname','$username','$password','$date')");

This post has been edited by VolcomMky: 17 February 2011 - 02:30 PM

Was This Post Helpful? 1
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 17 February 2011 - 02:28 PM

View PostVolcomMky, on 17 February 2011 - 04:25 PM, said:

2. Your SQL Query needs the column names then the values.


Actually, it doesn't, necessarily. It is better practice that it does (easier to track what data is being entered where), but if it doesn't, it will attempt to insert the values into the columns in the order in which they appear in the database.
Was This Post Helpful? 0
  • +
  • -

#9 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 17 February 2011 - 02:32 PM

You may be right, but better practice is what I've been nailed for on here when I posted without using better practice.

:surrender:
Was This Post Helpful? 0
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 17 February 2011 - 02:35 PM

Oh, that's why it's called better practice. But, it's still useful to know that it does work the other way, so you can spot it in non-functional code and know it isn't necessarily the culprit (unless of course you've got some mismatched data types in the process. That would certainly cause issues) :)
Was This Post Helpful? 0
  • +
  • -

#11 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 17 February 2011 - 02:38 PM

Of course, like a string in a Int ID field
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Guest*


Reputation:

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 17 February 2011 - 09:02 PM

Quote

Only time I've ever seen that access denied message in any language, not just PHP, is when the credentials for connecting to the database are incorrect.


I think this might be the case because I used these both times and tested them out and got the same error from before. If my credentials are incorrect, how can i go about fixing them?

With $id set, (non-auto-incrementing id column)
view source
print?
1	$queryreg = mysql_query("INSERT INTO users (IDCol, FullNameCol, UserNameCol, PassWordCol, DateRegCol) VALUES ('$id','$fullname','$username','$password','$date')");


Without $id set, (auto-incrementing id column)
view source
print?
1	$queryreg = mysql_query("INSERT INTO users (FullNameCol, UserNameCol, PassWordCol, DateRegCol) VALUES ('$fullname','$username','$password','$date')");

Was This Post Helpful? 0

#13 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Warning: mysql_connect() [function.mysql-connect]: error.

Posted 18 February 2011 - 12:45 AM

In your mysql_connect() call, you'd set the user and password as a valid user and password to connect to the MySQL database. In your specific case, it looks like you're using the wrong password for the root user. Also, it's highly discouraged to have the root user connect to the database, as it'll have access to literally everything in there, not just the database specific to that project/application.

This post has been edited by Valek: 18 February 2011 - 12:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1