Results 1 to 4 of 4

Thread: Running MySQL from .php files [resolved]

  1. #1

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090

    Running MySQL from .php files [resolved]

    I'm very new to PHP, I've read tutorials, been introduced to Mr. MySQL and tickered. I have been wondering, how to do you run MySQL code from a php file?

    All the tutorials I've read talk about using MS-DOS prompt to create the database. Well that is all fine and dandy, until I need a visitor to be able to add to my DB.

    Let's say I wanted to add a new record in a table called "mydb", where all the data is taken from textfields in the HTML. How would I do this?
    Last edited by Acidic; Feb 29th, 2004 at 11:44 AM.
    Have I helped you? Please Rate my posts.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    Assuming that you're using the POST method to send information from your HTML form to the PHP file, you can look at this code for a small reference:

    mysql_add.php:
    PHP Code:
    <?
      mysql_connect("localhost", "user", "pass");
      mysql_select_db("database_name");
      //make sure the form has been submitted
      if(!empty($_POST)){
        //do some error checking here if you want, to make
        //sure that all of the variables in $_POST are found
        //that you are requesting. this is not needed, but
        //does come in handy

        //POST variables can be retrieved with:
        //                $_POST['varname']
        //where 'varname' is retrieved from your HTML form's
        //"name" parameter.
        //ie: <input type='text' name='user'>
        //could be retrieved with $_POST['user']

        //if you're wanting to make some sort of registration
        //system, i suggest checking if some primary fields
        //are duplicates before you actually insert the info..
        //  (ie: usernames, id numbers, etc)

        //insert the database with your information
        mysql_query("INSERT INTO table_name (names, of, fields, to, insert, into) VALUES('values', 'of the', 'fields', 'that', 'you are', 'inserting', 'into')");

        //echo a success message
        echo "Successfully done something!\n";
      }else{
        echo "you can put your form here\n";
      }
    ?>
    It's untested, so you might have to make sure it works.. but it should work anyway. My comments might suck, along with my MySQL query.. ask questions if you have any about the above code.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Thank you very much, I'll test it out later when I have time. Those coments were very helpful.
    Have I helped you? Please Rate my posts.

  4. #4

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Had some problems, fixed them. never mind
    Last edited by Acidic; Feb 28th, 2004 at 07:49 AM.
    Have I helped you? Please Rate my posts.

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