Saving Submitted Data from Sign up page using PHP/MySQL

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.  
  6. $fName = $_POST['fName'];
  7. $lName = $_POST['lName'];
  8. $email = $_POST['email'];
  9. $reemail = $_POST['reemail'];
  10. $password = sha1($_POST['password']);
  11. $month = $_POST['month'];
  12. $day = $_POST['day'];
  13. $year = $_POST['year'];
  14. $gender = $_POST['optionsRadios'];
  15. $birthdate = $year . '-' . $month . '-' . $day;
  16.  
  17.  
  18. $query = "INSERT INTO tblmember(id, fName, lName, email, password, birthdate, gender)
  19. VALUES (NULL, '{$fName}', '{$lName}', '{$email}', '{$password}', '{$birthdate}', '{$gender}')";
  20.  
  21. if (mysql_query($query)) {
  22.  
  23. echo "<script type=\"text/javascript\">
  24. alert(\"New member added successfully.\");
  25. window.location = \"signup.php\"
  26. </script>";
  27.  
  28. } else
  29. 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. signuppage Then click the “Sign Up” button. After clicking the button a message will show. And it looks like as shown below. msgsuccess 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. signuptbl Take note, the password has been encrypted because we hash it on the “process_signup.php” for security purposes.

Comments

Submitted byJESSE KARIUKI … (not verified)on Wed, 04/15/2015 - 17:34

Hi GeePee. My Name is Jesse Kariuki. I noticed some errors in your Code. and is would love to submit some solution to it. This is in regards to mysql Deprecated Commands. Specifically Mysql_query,mysql_connect,mysql_select_db. Well for this Commands have Been deprecated and are no longer supported in this current Version of PHP we can as well employ the use of mysqli_query and also mysql_connect not forgetting also mysql_select_db. If you let me i can Propose some ammendments to the code and i will post this here, okay? Thank You. SIGNUP.PHP (Editted the Following Line)\ $connect_mysql = mysqli_connect("localhost", "root", "") or die(mysql_error()); $connect_db = mysqli_select_db($connect_mysql,"studentdb") or die(mysql_error()); Just edited the mysqli_connect by assigning a variable to it, the variable we will call it $connect_mysql I have also edited the mysql_select_db and assigned a variable also to it. well great code.

Add new comment