• We just launched and are currently in beta. Join us as we build and grow the community.

Saving Submitted Data from Sign up page using PHP/MySQL

LaBrute

Isekai Adventurer
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
157
Likes
114
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This tutorial is a continuation of our previous tutorial called How to create a Sign up Page Using twitter Bootstrap. This time we're going to focus on saving the data posted from the sign up page and we’re going to store it into the MySQL Database. To do this, we need to set first our database and here’s the table structure used in this tutorial.

  1. CREATE

    TABLE

    IF

    NOT

    EXISTS

    `tblmember`

    (
  2. `id`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `fName`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  4. `lName`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  5. `email`

    VARCHAR

    (

    50

    )

    NOT

    NULL

    ,
  6. `password`

    VARCHAR

    (

    60

    )

    NOT

    NULL

    ,
  7. `birthdate`

    text NOT

    NULL

    ,
  8. `gender`

    VARCHAR

    (

    20

    )

    NOT

    NULL

    ,
  9. PRIMARY

    KEY

    (

    `id`

    )
  10. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1 AUTO_INCREMENT

    =

    6

    ;

Next, open our PHP file named “process_singup.php” then, we’re going to change all the codes and here’s it looks like:

  1. //set up mysql connection
  2. mysql_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  3. //select database
  4. mysql_select_db

    (

    "studentdb"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;

  5. $fName

    =

    $_POST

    [

    'fName'

    ]

    ;
  6. $lName

    =

    $_POST

    [

    'lName'

    ]

    ;
  7. $email

    =

    $_POST

    [

    'email'

    ]

    ;
  8. $reemail

    =

    $_POST

    [

    'reemail'

    ]

    ;
  9. $password

    =

    sha1

    (

    $_POST

    [

    'password'

    ]

    )

    ;
  10. $month

    =

    $_POST

    [

    'month'

    ]

    ;
  11. $day

    =

    $_POST

    [

    'day'

    ]

    ;
  12. $year

    =

    $_POST

    [

    'year'

    ]

    ;
  13. $gender

    =

    $_POST

    [

    'optionsRadios'

    ]

    ;
  14. $birthdate

    =

    $year

    .

    '-'

    .

    $month

    .

    '-'

    .

    $day

    ;


  15. $query

    =

    "INSERT INTO tblmember(id, fName, lName, email, password, birthdate, gender)
  16. VALUES (NULL, '{$fName}

    ', '{$lName}

    ', '{$email}

    ', '{$password}

    ', '{$birthdate}

    ', '{$gender}

    ')"

    ;

  17. if

    (

    mysql_query

    (

    $query

    )

    )

    {

  18. echo

    "<script type=\"

    text/javascript\"

    >
  19. alert(\"

    New member added successfully.\"

    );
  20. window.location = \"

    signup.php\"
  21. </script>"

    ;

  22. }

    else
  23. die

    (

    "Failed: "

    .

    mysql_error

    (

    )

    )

    ;

In the code above, we set first the MySQL Connection and we choose our database name in our case I use “studentdb”. Then we create a variable and we populate the variable using the “superglobal” variable $_POST containing some initial information. Next, we create an “INSERT INTO” statement and store it into $query variable, then we use mysql_query () function to execute a query on a MySQL Database. Then if the data are successfully saved to our MySQL database, it will be redirected back to SignUp page.

This time, we are going to test our application. First, open any browser then on the address bar type http://localhost/signup/signup.php and fill up the sign up page. It should look like as shown below.

signuppage1_0.png


Then click the “Sign Up” button. After clicking the button a message will show. And it looks like as shown below.
msgsucces.png


And when you click the “OK” button you will be redirected to Sign Up page. Next, we’re going to check if the data has been saved to the database. We will open the phpmyadmin and view the “tblmember” table. And the output should like as shown below.
singuptble.png


Take note, the password has been encrypted because we hash it on the “process_signup.php” for security purposes.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top