Putting It All Together

Ello' Mattie ...

Please note that this tutorial is part of the Build Your Own Site series of tutorials. Some parts of this tutorial rely on some of our previous lessons.

We built our own database in the last tutorial. It was complete with a table and several fields. In this tutorial we're going to connect your PHP code, or rather your Apache server to that database. Then we'll save the form submitted data into that database.

Excited?!

"Alright, alright, alright ..." - Kevin Hart's Dad

The code below is used to connect to the database. Please note that this is as bare as a database connection can go. Do not use this example in your production environment.

<?php
  $servername = "localhost";
  $username = "rooty_name";
  $password = "password";

  $conn = new mysqli($servername, $username, $password);

  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  }
  print_r("Connected Like a mother!!");
?>

Load the page and witness your successful connection!! ... Please note that we used the MySQLi driver, the next code we'll show how to use the PDO one. Like this:

<?php
    $servername = "localhost";
    $username = "root_username";
    $password = "password";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected like a preacher to heaven!!";
        }
    catch(PDOException $e)
        {
        echo "Connection failed like my recent math class: " . $e->getMessage();
        }
?>

Try mess around with that code will ya :) ... See it fail and succeed, get comfortable enough to change the variable names. That Doing this will remove some of the anxiety about writing something like this.

Alright ... Now all that is left to do, is to write the code that saves the form submitted data to our database.

<?php
    $servername = "localhost";
    $username = "root";
    $password = "password";
    $database = "pressphp";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";

    $name = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $sql = "INSERT INTO form (firstname, lastname, email)
    VALUES ($name, $lastname, $email)";

    if ($conn->query($sql) === TRUE) {
      echo "User inserted";
    } else {
      echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
?>

The PDO version will look something like this:

<?php
    $servername = "localhost";
    $username = "root_username";
    $password = "password";
    $database = 'pressphp';

    $name = $_POST['name'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];

    try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ($name, $lname, $email)";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "User inserted ...";
    }
    catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

    $conn = null;
?>



Good there you go soldier!! You have created a database, made a connection to it, and are now actually saving data to it.

Alright load your HTML form as discussed in the previous 5 tutorials. Fill out the form and press the submit button.

Once it's done saving the data into the database you should see the text User inserted.

"Alright, alright, alright ..." - Matthew McConaughey

Once again, please play around as much as you can to get a bit more comfortable with all this new information. Also note that we have only written code to save the first three fields of the entire form. So take some time to add the fields on the form to both the mysql commands, as well as the database form table list of fields.

"You have created a database, made a connection to it, and are now actually saving data to it."

The Village Geek

If you find yourself a bit lost on some of these concepts, please feel free to go over these tutorials again to recall and cement this information into that pretty brain of yours. You'll be amazed at just how much helpful repetition is.

Ayytt ciao soldier!

Cape Town, South Africa

Buy me a coffee ? :) Buy me a coffee :)

Reply to this discussion

Bookmarks

Build
Learn
Coming Soon ...