Results 1 to 10 of 10

Thread: Newbie php addind data to mysql

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Newbie php addind data to mysql

    ok i first tried this.....

    Code:
    <form action="insert.php" method="post">
    
    UserName: <input type="text" name="username"><br>
    Password: <input type="text" name="password"><br>
    
    <input type="Submit">
    </form>
    and in insert.php....

    PHP Code:
    <?


    $database="users";

    mysql_connect() or die ("Problem connecting to DataBase");

    @mysql_select_db($database) or die( "Unable to select database");

    $query = "INSERT INTO info VALUES ('$username','$password')"; 
    mysql_query($query);

    mysql_close();

    ?>
    i've been looking at these global varible things but they still not making sence to me can anyone tell em how to solve this? all iw ant to do is add data to mysql

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Try to change the insert line to:


    Code:
    $query = mysql_query("INSERT INTO info VALUES('$username','$password')");


    BTW what is the error message?


    Is there only two fields in that table?

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You should also make the password input of type password (makes the stars show).

    Oh, and I scan the forums almost daily, there's no need to PM me.
    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.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    Notice: Undefined variable: username in (document path)insert.php on line 11

    Notice: Undefined variable: password in (document path)insert.php on line 11

    thanks

    sry botut th epm its just i've been trying for nerly 5 days to learn

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You're trying to use the deprecated (for security reasons) and by default disabled feature of PHP to offer all passed data as global variables. As of new PHP versions, you can access the data with
    $_GET['fieldname'] (if the form has method="get")
    and
    $_POST['fieldname'] (if the form has method="post")
    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.

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Looks werid...if there is a password or something on the database try to connect to it like this:

    PHP Code:
    $db mysql_pconnect ("localhost""name_of_the_user""password_to_the_database");

    if (!
    $db){
        echo 
    'I cannot connect to the database because: ' mysql_error();
        exit;
    }

    mysql_select_db ("name_of_the_database"); 
    [Edit]Forget about this: Listen to the man that know this.....[/Edit]

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    how would i incorperate that into my problem? sorry for i'm still learning

  8. #8
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Posted by NoteMe
    PHP Code:
    mysql_error(); 
    Ar so thats how you find out what went wrong in a DB call . I've been wondering how to find that out, suppose mysql_error() does kinda make sense .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    where do i put the post thing? im confused please help me!

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Rewrite the $query = line like this:
    $query = "INSERT INTO info VALUES ('".$_POST['username']."','".$_POST['password']."')";

    It's possible to do it without concatenating strings, but in case of arrays I don't like doing this for several reasons, one of which is that it breaks the syntax highlighting of jEdit
    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