the basics of using databases

Post on 06-Jan-2016

33 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

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

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

• Keep database (or multiple databases) on your server

• Create tables in your database

Database Tables

ID Name E-mail Address1 John Doe jdoe@gmail.com2 Jacob Bredthauer jabredth@mit.edu3 Jane Doe jane@yahoo.com

Email Addresses

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

Favorite Bands

phpMyAdmin

Making a Form

Input tags

<input type=“text” />

Types:

“text”

“radio”

“checkboxes”

“submit”

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

Make the last one a “submit” button.

<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>

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.

What is PHP?

• PHP is programming language

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

• PHP can be used to interface with MySQL easily

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

Beginning the PHP file

<?php

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

Server Address Username Password

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`];

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 ...

xkcd.com

Escaping the Input

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

Be careful!

Questions?

top related