Jump to content

Recommended Posts

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)

Link to comment
https://forums.phpfreaks.com/topic/38415-php-formnot-connecting-to-db/
Share on other sites

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"; }
?>

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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