How To Insert Data in to a MySQLi Database through PHP

Introduction: This tutorial will be on allowing users to input data in to an HTML form, and then upload that data to a MySQL database through PHP. HTML: First we need our HTML form. This example will just be for a user to enter their username and email address, we will not do any validation or username checking (to assure no duplicate usernames within our website) as this is not the purpose of this tutorial. In fact, we can use the 'email' input type as opposed to 'text' for the email address to ensure the user enters a correctly formatted address ([email protected] - @ and . in the correct places).
  1. <head>
  2. </head>
  3. <body>
  4. <form action='page.php' action='POST'>
  5. <input type='text' value='Username' name='username' /><br/>
  6. <input type='email' value='Email' name='email' /><br/>
  7. <input type='submit' value='Submit' name='userSubmit' />
  8. </form>
  9. </body>
  10. </html>
This form allows the user to enter a username and email address which will be sent as POST data to the page 'page.php'. Database: Before we can add our data through PHP to our database tables, we need the database and tables to already be created. So in your localhost/web host PHPMyAdmin, create a new database, mine will be called 'testDB', and in that create a table named 'testTable' with the following columns; id, int, 5 length, primary key, auto increment username, text, 255 length email, text, 255 length PHP: Now for our PHP processing of our HTML form. First we make the connection to our database, this conection will be stored in a variable named 'con' and the 'mysqli_connect' function takes four parameters; service server, username, password, database name. So I would connect to my localhost with the username as 'root' and no password like so;
  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'testDB');
  3. ?>
Next we want to ensure that our user form information is set correctly. If not, we output an error...
  1. if (isSet($_POST['userSubmit'])) { //Check if form is submitted. No else, otherwise normal page loads would show an error all the time.
  2. if (isSet($_POST['username']) && isSet($_POST['email']) && $_POST['username'] != '' && $_POST['email'] != '') {
  3.  
  4. }else
  5. echo 'Not all the information was set correctly. Please try again.';
  6. }
If all the information is correct, we can now declare the information...
  1. $user = $_POST['username'];
  2. $email = $_POST['email'];
And then insert the information in to our database, like so...
  1. $q = mysqli_query($con, "INSERT INTO `testTable` VALUES ('', '$user', '$email')");
As you can see from the 'mysqli_query' function above, it takes two parameters; a connection to the mysqli database, and a query to execute as a string. The query we execute inserts the information; '' (nothing), '$user' (username), '$email' (email) in to our 'testTable' table within our database we set within our connection variable/object. The final thing we want to do is to check if the query executed correctly, and if not, output an error...
  1. if ($q) {
  2. echo 'Inserted data successfully.';
  3. }else
  4. echo 'Failed to insert data. ';
Finished!

Comments

Submitted bykyle (not verified)on Fri, 08/15/2014 - 06:30

How would I do this but witj radio buttons instead?

Add new comment