Jump to content

mysql database connection problem


martinstan

Recommended Posts

Hi
I'm doing a tutorial at the minute and I'm trying to connect to a mysql database and get the following error
[code]Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: NO) in C:\wamp\www\vtc\send_feedback.php on line 9
Error connecting to mysql
[/code]

It really has me stumped. Can anyone please help? Below is my php code
[code]<?
$dbhost = 'localhost';
$dbuser ='root';
$dbpassword = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
('Error connecting to mysql');
$dbname = "vinyldealers";
mysql_select_db($dbname);

if(!$user || !$email || !$comments){
?>
<h2>Whoops</h2><BR>Please fill in all the fields.
<br><a href="feedback.html">Click here to go back.</a>
<? exit;
}
$user = addslashes($user);
$email = addslashes($email);
$comments = addslashes($comments);
$addfeedback = "INSERT INTO feedback (user, email, spam, comments)
VALUES ('".$user."', '".$email."', '".$spam."', '".$comments."')";
$result = mysql_query($addfeedback);
?>
<h2>Thankyou</h2>
<BR>
We have added your comments to our database.

</body>

</head></html>[/code]

Thanks in advance
Martin
Link to comment
https://forums.phpfreaks.com/topic/26951-mysql-database-connection-problem/
Share on other sites

Thanks for your amazingly quick reply.
I've now set the variable to $pass and I no longer get the error, however I don't think that the connection is now being made to the database. Even though I'm filling in my form fields, I'm now just getting my 'whoops' html message. I don't know-here is my form too.

[code]<html>
<head>
<title>Vinyl Dealers: Feedback</title>
</head>
<body>
<h2>Feedback</h2>
<br/>
<FORM action="send_feedback.php" method="post">
Your name:
<input type=text name"user" maxlength="40" size="40">
<br>
Your email:
<input type=text name"email" maxlength="40" size="40">
<br><br>
Can we keep you updated with news about our site?
<input name="spam" type="radio" value="1" checked>Yes
<input name="spam" type="radio" value="0">No
<br><br>
Comments:
<BR>
<textarea cols="60" rows="10" name="comments"></textarea>
<BR>
<input type= submit value="submit">
</FORM>
</body>
</html>[/code]
You got a couple of different problems.

First on your form you  named some of your fields like this: [b]name"user"[/b]
That won't work, you need to do it like this: [b]name="user"[/b]

Then, unless there is additional code you have not posted the other problem is that in the code that processes the form data (the first one you posted) you are using the variables: $user, !$email, !$comments.

Those are local variables and you have not set them anywhere that I can see. You need to reference the form fields within the $_POST array.

So, a field on the form called "user" can be refereneced on the receiving page as "$_POST[user]"
Hi
sorry it's taken me a bit of time to reply I've been out for the evening. Sounds quite complicated and as I said I'm following along with a tutorial so the advice I'm being given must be a bit dodgy! I'll have a good look at it tomorrow and post again. Thanks for your time.
Martin
mjdamato
thanks for your help.
Jeez this programming is frustrating. The whole problem revolved around me ommiting the = from the name="user" etc.. as you rightly pointed out. Having said that I'm pretty good at CSS and I know how missing the small bits of detail effects that too, so  I should expect it really.
All working fine, however I'm not sure how my CD tutor managed to get his to work so well as he had it missing from his code. ;)
Thanks again
Martin

Archived

This topic is now archived and is closed to further replies.

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