180 likes | 307 Views
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
E N D
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 Email Addresses Favorite Bands
Making a Form Input tags <input type=“text” /> Types: “text” “radio” “checkboxes” “submit”
Making a Form Put 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 PHPfile.
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 database mysql_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 ...
Escaping the Input $email = mysql_real_escape_string($_POST[`email`]); Be careful!