Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

unable to connect to sql with a form

sql form

8:49 am on May 7, 2011 (gmt 0)

New User

5+ Year Member

joined:May 7, 2011
posts: 5
votes: 0

So, I'm painfully new at all of this. The last time I even thought about playing around with websites, angelfire was the new hotness and everyone was getting in on some sixdegrees...

Well, I'm trying to learn by doing, but I've ran into a roadblock. I'm attempting to make a simple random quote generator. I've got the MySQL db made, configured, and user set. I decided to try to make a form so populating it would be easier. When I load the URL for the form, I get this error:
Access denied for user 'dbusername'@'localhost' (using password: YES) in /home/hostusername/public_html/quoteform.php on line 18

Here is the code I'm using:
<form action="" method="post"> 
<legend>Add a Quote</legend>
<label for="quote">Quote:</label>
<input type="text" name="quote" id="quote" maxlength="1500" />
<label for="author">Author:</label>
<input type="text" name="author" id="author" maxlength="60" />
<input type="submit" value="Add Quote" />

$connection = mysql_connect('localhost',$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

if ($_REQUEST['quote'] != "") {
if($_REQUEST['author'] != "") {
$author = $_REQUEST['author'];
} else {
$author = "Anonymous";
$quote = $_REQUEST['quote'];

$query="INSERT INTO `quote` (`quote`,`source`) values ('" . mysql_real_escape_string($quote) . "','" . mysql_real_escape_string($author) . "')";
$result = mysql_query($query) or die(mysql_error());
echo("inserted quote: " . htmlentities($quote) . " by " . htmlentities($author) . " into database");
} else {
echo("<p>Please enter a quote and author</p>");

Now, I freely admit to having ganked this code off of a blog (I would include the blog's url, but I'm not sure if that's kosher.) in an attempt to learn by fiddling with, but I'm at a loss.

Oh, I've a shared host at hostgator if that makes any sort of difference.

11:37 pm on May 7, 2011 (gmt 0)

Senior Member

joined:Nov 12, 2005
votes: 0

Hello and Welcome to WebmasterWorld!

Assuming you are using the correct host, username, and password, then my second guess would be that you are using an older mysql library that would require to use an older version of the MySQL passwords. If you can run a query in your database, try doing the following as root (or equivalent):

Password = OLD_PASSWORD('dbpw')
Host = 'localhost'
AND User = 'mydbusername';

And then flush the privileges:

6:37 pm on May 8, 2011 (gmt 0)

New User

5+ Year Member

joined:May 7, 2011
posts: 5
votes: 0

Thank you for the reply.

I managed to get it fixed based on your first suggestion. I created a new db user just for that database (I had assigned an existing one prior, with what I thought were full permissions, but obviously were not for whatever reason), and it started working just fine.

Heh, now on to the joys of attempting to figure out how to integrate javascript, php, and html :)