Albatross Posted February 14, 2007 Share Posted February 14, 2007 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: <? $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) Quote Link to comment https://forums.phpfreaks.com/topic/38415-php-formnot-connecting-to-db/ Share on other sites More sharing options...
benjaminbeazy Posted February 14, 2007 Share Posted February 14, 2007 try this... <? //database $db_host="localhost"; $db_name="dbname"; $db_username="username"; $db_password="password"; $database = mysql_connect($db_host,$db_username,$db_password) or die ('cannot connect to the //database because: ' . mysql_error()); mysql_select_db($db_name,$database) or die ('cannot select db: ' . 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); if($result){ echo "you win"; }else{ "you lose"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/38415-php-formnot-connecting-to-db/#findComment-184267 Share on other sites More sharing options...
Albatross Posted February 14, 2007 Author Share Posted February 14, 2007 Thank you for your reply. While it didn't get me in, it did tell me that I had a username/password error and is not something I have to take up with my ISP. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/38415-php-formnot-connecting-to-db/#findComment-184313 Share on other sites More sharing options...
benjaminbeazy Posted February 14, 2007 Share Posted February 14, 2007 always good to have right username and password Quote Link to comment https://forums.phpfreaks.com/topic/38415-php-formnot-connecting-to-db/#findComment-184338 Share on other sites More sharing options...
Albatross Posted February 17, 2007 Author Share Posted February 17, 2007 It's me again The origional problem was that I was trying to connect across 2 diferent domains, and was specifying a local connection, not a remote one. The fix was in my $connection to use ("www.domain.com", "username", "password") or die... but here's a whole new issue for me... Trying to load data to the db from a form and it's going there, but the fields are all shifted to one side. I'll give an example : my code looks something like this : $sql = "INSERT INTO $table_name (name, history, recruiting, email, im, charname, charage, bio, strengths, whatship, responsibilities, sample, rank, position, role, sim, reportsto, comments, approved, ban, reserved) VALUES ('', '$_POST[name]', '$_POST[history]', '$_POST[recruiting]', '$_POST[email]', '$_POST[im]', '$_POST[charname]', '$_POST[charage]', '$_POST[bio]', '$_POST[strengths]', '$_POST[whatship]', '$_POST[responsibilities]', '$_POST[sample]', '$_POST[rank]' '$_POST[position]', '$_POST[role]', '$_POST[sim]', '$_POST[reportsto]', '$_POST[comments]', '$_POST[approved]', '$_POST[ban]', '$_POST[reserved]')"; $result = @mysql_query($sql, $connection) or die(mysql_error()); and what I get in my db is this : Header: name history recruiting email Data: name history recruiting Any suggestions on what Im doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/38415-php-formnot-connecting-to-db/#findComment-187392 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 VALUES ('', remove the '', Quote Link to comment https://forums.phpfreaks.com/topic/38415-php-formnot-connecting-to-db/#findComment-187398 Share on other sites More sharing options...
Albatross Posted February 17, 2007 Author Share Posted February 17, 2007 Did just that and got this : Column count doesn't match value count at row 1 Not sure what else to try... Quote Link to comment https://forums.phpfreaks.com/topic/38415-php-formnot-connecting-to-db/#findComment-187403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.