PHP form/not connecting to db
I'm a little stumped here because I've been doing it right along...I write a little application, data gets put into it, user hit's submit, it get's fed to the db and a result page is shown (usually with the submitted data so user can correct if they find errors)
Problem this time is I'm getting an error. Here's the code:
PHP Code:
<?
$db_name = "dbname";
$table_name = "table";
$connection = @mysql_connect("localhost", "username", "password") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "INSERT INTO $table_name (Name, Age, History, Recruiting, Email, IM, Charname, Charage, Bio, Strengths, WhatShip,
Responsibilities, Sample) VALUES ('$_POST[Name]', '$_POST[Age]', '$_POST[History]', '$_POST[Recruiting]', '$_POST[Email]',
'$_POST[IM]', '$_POST[CharName]', '$_POST[Charage]', '$_POST[Bio]', '$_POST[Strengths]', '$_POST[WhatShip]',
'$_POST[Responsibilities]', '$_POST[Sample]')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
?>
And here is the error when I hit submit :
Access denied for user 'username'@'localhost' to database 'dbname'
If someone would like to see firsthand the sheet i'm using, please go to http://www.ussretribution.com/coappli.htm The submit button is linked to the code above. The rest of the sheet that contains the php code is just the return page showing the user what they did.
Quite frankly, I can't see why it doesn't work and am at a loss here. Any help anyone can offer would be greatly appreciated. (Just got haircut so none left to pull out)
Re: PHP form/not connecting to db
well, you aren't logging into your mysql server with a correct username and password. "username" does not have the sufficient access rights to either a: even connect to the server, or b: if it can connect to the server, it doesn't have access privileges for the database "dbname." do you have your mysql server set up? with a username and password to login with? did you literally copy and paste that code, or did you replace your actual username/password/table/database with generic names? if the former, you need to specify a valid database name, table name, user name and user password for the server before you can interface with it.
also, you should probably do at least a little error checking on your $_POST variables before you blindly insert them, too.
Re: PHP form/not connecting to db
You should simply scrap the old MySQL API in favour of PEAR::MDB2 if you're still using PHP4 or PDO in PHP5, and use parametrized queries. Validation is so easy to get wrong or forget.