the basics of using databases
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 PresentationTRANSCRIPT
![Page 1: The Basics of Using Databases](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/1.jpg)
The Basics of Using Databases
![Page 2: The Basics of Using Databases](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/2.jpg)
What is a Database?
• A place to store information
• Website can add or retrieve information
![Page 3: The Basics of Using Databases](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/6.jpg)
phpMyAdmin
![Page 7: The Basics of Using Databases](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/7.jpg)
Making a Form
Input tags
<input type=“text” />
Types:
“text”
“radio”
“checkboxes”
“submit”
![Page 8: The Basics of Using Databases](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/8.jpg)
Making a FormPut multiple “inputs” inside <form> </form> tags.
Make the last one a “submit” button.
![Page 9: The Basics of Using Databases](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/9.jpg)
<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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/13.jpg)
Beginning the PHP file
<?php
mysql_connect(“sql.mit.edu”, “jacob”, “psswd”);
Server Address Username Password
![Page 14: The Basics of Using Databases](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/16.jpg)
xkcd.com
![Page 17: The Basics of Using Databases](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/17.jpg)
Escaping the Input
$email = mysql_real_escape_string($_POST[`email`]);
Be careful!
![Page 18: The Basics of Using Databases](https://reader036.vdocuments.site/reader036/viewer/2022081603/56813b47550346895da42afc/html5/thumbnails/18.jpg)
Questions?