Results 1 to 3 of 3

Thread: PHP form/not connecting to db

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    62

    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)

    "If at first you don't succeed, destroy all evidence you ever tried."
    http://www.ussretribution.com

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width