the basics of using databases

18
The Basics of Using Databases

Upload: vevay

Post on 06-Jan-2016

33 views

Category:

Documents


2 download

DESCRIPTION

The Basics of Using Databases. What is a Database?. A place to store information Website can add or retrieve information. 3-Step Process. Create a database Make a form on a webpage Set up webpage to send information to the database. Creating a Database. Use program like MySQL - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Basics of Using Databases

The Basics of Using Databases

Page 2: The Basics of Using Databases

What is a Database?

• A place to store information

• Website can add or retrieve information

Page 3: The Basics of Using Databases

3-Step Process

• Create a database

• Make a form on a webpage

• Set up webpage to send information to the database

Page 4: The Basics of Using Databases

Creating a Database

• Use program like MySQL

• Keep database (or multiple databases) on your server

• Create tables in your database

Page 5: The Basics of Using Databases

Database Tables

ID Name E-mail Address1 John Doe [email protected] Jacob Bredthauer [email protected] Jane Doe [email protected]

Email Addresses

ID Person Favorite Band1 1 Rolling Stones2 1 Aerosmith3 3 Backstreet Boys4 2 Styx5 2 Switchfoot

Favorite Bands

Page 6: The Basics of Using Databases

phpMyAdmin

Page 7: The Basics of Using Databases

Making a Form

Input tags

<input type=“text” />

Types:

“text”

“radio”

“checkboxes”

“submit”

Page 8: The Basics of Using Databases

Making a FormPut multiple “inputs” inside <form> </form> tags.

Make the last one a “submit” button.

Page 9: The Basics of Using Databases

<form><dl><dt>Text: </dt><dl><input type="text" /></dl><dt>Buttons: </dt><dd><input type="radio" value="Option 1" />Option 1<input type="radio" value="Option 2" />Option 2</dd><dt>Checkboxes: </dt><dd><input type="checkbox" value="Option 1" />Option 1<input type="checkbox" value="Option 2" />Option 2</dd><dt>Submit button: </dt><dd><input type="submit" value="submit" /></dd></dl></form>

Page 10: The Basics of Using Databases

Making the Form Useful

Add attributes to the form tag:

<form action=“sendToDatabase.php” method=“post”>

...

</form>

Add attributes to the input tags:

<input type=“text” name=“email” />

We will need to make a PHP file.

Page 11: The Basics of Using Databases

What is PHP?

• PHP is programming language

• PHP files begin with <?php and end with ?>

• PHP can be used to interface with MySQL easily

Page 12: The Basics of Using Databases

What our PHP file needs to do

• Connect to our database

• Get that data from the form

• “Escape” the data so it’s safe

• Send the data to the database

Page 13: The Basics of Using Databases

Beginning the PHP file

<?php

mysql_connect(“sql.mit.edu”, “jacob”, “psswd”);

Server Address Username Password

Page 14: The Basics of Using Databases

Getting the data from the form

Recall the attributes we added to the form and the inputs:<form action=“sendToDatabase.php” method=“post”><input type=“text” name=“email” />

$person = $_POST[`person`];$email = $_POST[`email`];

Page 15: The Basics of Using Databases

Sending data to the databasemysql_query(“INSERT INTO `mydatabase`.`table1` (`ID`, `Name`, `Email`) VALUES (NULL, $name, $email)”);

Queries can perform many different functions, including adding to the database, reading from the database, or ...

Page 16: The Basics of Using Databases

xkcd.com

Page 17: The Basics of Using Databases

Escaping the Input

$email = mysql_real_escape_string($_POST[`email`]);

Be careful!

Page 18: The Basics of Using Databases

Questions?