Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2007
    Posts
    1,683
    Tokens
    0

    Latest Awards:

    Default PHP/MySQL form tutorial.

    Right, I was bored at the time of making this.

    PHP Code:
    <?php
    ## CONNECTION TO DATABASE ##
    mysql_connect("Your host""Your username""Your password"); // Connecting to the allocated server with the credentials inserted. (hostname, user, pass)
    mysql_select_db("Your database"); // Selecting the database we'll connect too.
    ## VARIBLES ##
    $table "lolcopter"// My table name is lolcopter.. Update this?
    $field "waffle"// My field name is waffle.. Update this?
    ## CLEANER ##
    function clean($s) { // Function start.
    if (get_magic_quotes_gpc()) // Performing the clean, basically.
    $s stripslashes($s); // Stripping the slashes from the public seeing \"lol or so forth.
    return mysql_real_escape_string($s); // Returning it..
    // Ending the function. As you see later in the code "clean($_POST..".
    ## FORM ## - Just to make our code cleaner, I've sectioned it.
    if(!$_POST['submit']) { 
    echo 
    "<form action=\"\" method=\"post\"><input name=\"roflcopter\" type=\"text\" /><input name=\"submit\" type=\"submit\" /></form>"// Showing the form that will submit the data "roflcopter".
    // End of the !$_POST['Submit'] statement.
    else { // If it has been posted, then the "else" will attempt to insert it into the database.
    ###############################################
    $roflcopterpost clean($_POST['roflcopter']); // We're cleaning the posted message, before it's inserted into the database.
    mysql_query("INSERT INTO "$table ." ("$field .") VALUES('$roflcopterpost') ") or die("Error executing your form."); // Now we're inserting it into the database.
    echo "The data has been entered into the database."// We're stating that it's been inserted.
    ################################################
    }
    ?>
    I've commented it as much as I could;

    First, create a database;
    Second, enter the connection details;
    Third, Create a table & field named Waffle or something. (phpmyAdmin)?;
    Forth, Insert the field name into " $field ".

    This was a tutorial just to show you basically how it works, you can add-on bits, there are many useful tutorials all over the web.. some other links are listed below.

    http://www.tizag.com/mysqlTutorial/
    http://www.tizag.com/phpT/
    http://www.techtuts.com/forums/index.php?showforum=5

    Hope I've helped..
    Calon.
    Last edited by Independent; 07-07-2008 at 12:55 AM.

  2. #2
    Join Date
    Nov 2006
    Location
    Liverpool/Manchester
    Posts
    2,457
    Tokens
    0

    Latest Awards:

    Default

    Works Since you changed it

    Thanks

    http://nexdana.com/test.php
    Last edited by iJoe; 07-07-2008 at 01:10 AM.
    Joe


  3. #3
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Would be nice if even I could understand it lols

    This would be a better example on your behalf.

    PHP Code:
    <?php
    /* Database connection information */
    mysql_connect 'localhost''username''password' ) or
    die ( 
    'Cannot connect to the database, reason: ' mysql_error () );

    mysql_select_db 'database_select' );

    /* Clean func */
    function cleanString $str )
    {
        
    // You could add more cleaning functions here if you wish.
        
    $_str mysql_real_escape_string trim $str ) );
        return ( 
    $_str );
    }

    /* Weird way of confirming post... */
    if ( !$_POST 'submit' ] )
    {
        echo ( 
    '<form action="" method="post">
    <input name="textfield" type="text">
    <input name="submit" value="submit" type="submit">
    </form>' 
    );
    }
    else
    {
        if ( 
    $_POST'textfield' ] != '' )
        {
            
    $mysqlQuery mysql_query 'INSERT INTO `table` ( `field` ) VALUES ( "' cleanString $_POST'textfield' ] ) . '" );' );
            if ( 
    $mysqlQuery )
            {
                echo ( 
    'Complete' );
            }
            else
            {
                echo ( 
    'Error' );
            }
        }
        else
        {
            echo ( 
    'Please do not leave the field blank!' );
        }
    }
    ?>
    Last edited by Protege; 07-07-2008 at 01:12 AM.
    Hi, names James. I am a web developer.

  4. #4
    Join Date
    Dec 2007
    Posts
    1,683
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Protege View Post
    Would be nice if even I could understand it lols

    This would be a better example on your behalf.

    PHP Code:
    <?php
    mysql_connect 
    'localhost''username''password' ) or
    die ( 
    'Cannot connect to the database, reason: ' mysql_error () );

    mysql_select_db 'database_select' );

    function 
    cleanString $str )
    {
        
    // You could add more cleaning functions here if you wish.
        
    $_str mysql_real_escape_string trim $str ) );
        return ( 
    $_str );
    }

    if ( !
    $_POST 'submit' ] )
    {
        echo ( 
    '<form action="" method="post">
    <input name="textfield" type="text">
    <input name="submit" value="submit" type="submit">
    </form>' 
    );
    }
    else
    {
        if ( 
    $_POST'textfield' ] != '' )
        {
            
    $mysqlQuery mysql_query 'INSERT INTO `table` ( `field` ) VALUES ( "' cleanString $_POST'textfield' ] ) . '" );' );
            if ( 
    $mysqlQuery )
            {
                echo ( 
    'Complete' );
            }
            else
            {
                echo ( 
    'Error' );
            }
        }
        else
        {
            echo ( 
    'Please do not leave the field blank!' );
        }
    }
    ?>
    Thanks, that's a hell of a lot more cleaner.

    But I think my tutorial explains a little better. ^_^

  5. #5
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Yes but you don't want to teach people evil hard-to-read code.
    Hi, names James. I am a web developer.

  6. #6
    Join Date
    Nov 2006
    Location
    Liverpool/Manchester
    Posts
    2,457
    Tokens
    0

    Latest Awards:

    Default

    How would I add another field?

    I tried, well just kinda guessed

    PHP Code:
    <?php
    ## CONNECTION TO DATABASE ##
    mysql_connect("localhost""nexdana_test""????"); // Connecting to the allocated server with the credentials inserted. (hostname, user, pass)
    mysql_select_db("nexdana_test"); // Selecting the database we'll connect too.
    ## VARIBLES ##
    $table "waffle"// My table name is waffle.. Update this?
    $field "lolcopter"// My field name is lolcopter.. Update this?
    $field2 "shoe"// My second field name is shoe
    ## CLEANER ##
    function clean($s) { // Function start.
    if (get_magic_quotes_gpc()) // Performing the clean, basically.
    $s stripslashes($s); // Stripping the slashes from the public seeing \"lol or so forth.
    return mysql_real_escape_string($s); // Returning it..
    // Ending the function. As you see later in the code "clean($_POST..".
    ## FORM ## - Just to make our code cleaner, I've sectioned it.
    if(!$_POST['submit']) { 
    echo 
    "<form action=\"\" method=\"post\"><input name=\"roflcopter\" type=\"text\" /><input name=\"shoe\" type=\"text\" /><input name=\"submit\" type=\"submit\" /></form>"// Showing the form that will submit the data "roflcopter".
    // End of the !$_POST['Submit'] statement.
    else { // If it has been posted, then the "else" will attempt to insert it into the database.
    ###############################################
    $roflcopterpost clean($_POST['roflcopter']); // We're cleaning the posted message, before it's inserted into the database.
    mysql_query("INSERT INTO "$table ." ("$field $field2 .") VALUES('$roflcopterpost')('$shoepost') ") or die("Error executing your form."); // Now we're inserting it into the database.
    echo "The data has been entered into the database."// We're stating that it's been inserted.
    ################################################
    }
    ?>
    Joe


  7. #7
    Join Date
    Dec 2007
    Posts
    1,683
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Protege View Post
    Yes but you don't want to teach people evil hard-to-read code.
    *Shifty looks*

    True, but it shows someone how at least. ^_^

    I'm going to bed now, hope this helps someone or gets moved to tutorial section

Posting Permissions

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