Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Photo credit: Phil Synder cc
Some parts of your code are repeated over and over. We can move these into their own files and use include
to call them.
Let's improve our coffeeshop page with some styles. Download the starter files as a .zip.
Thanks to Amy Hendrix for the code.
Play with the site code.
Photo credit: Brenda-Starr cc
Let's edit first database. Start up WAMP/MAMP and go to http://localhost/phpmyadmin/ (Windows) or http://localhost:8888/phpmyadmin/ (Mac)
The default username is "root". The default password is either "" (i.e., leave it blank) or "root".
USE coffee;
DROP TABLE product;
CREATE TABLE product (
productID int NOT NULL AUTO_INCREMENT,
companyID_fk int,
type varchar(255),
roast varchar(255),
description text,
PRIMARY KEY (productID)
);
CREATE TABLE company (
companyID int NOT NULL AUTO_INCREMENT,
name varchar(255),
phoneNumber varchar(255),
PRIMARY KEY (companyID)
);
ALTER TABLE product ADD INDEX (companyID_fk);
ALTER TABLE product
ADD CONSTRAINT FK_company FOREIGN KEY (companyID_fk)
REFERENCES company (companyID)
ON DELETE CASCADE ON UPDATE CASCADE;
INSERT INTO company SET name='Starbucks', phoneNumber='(800)555-7282';
INSERT INTO company SET name='Bean Traders', phoneNumber='(919)555-5895';
INSERT INTO company SET name='Mean Beans', phoneNumber='(303)555-8475';
INSERT INTO product SET companyID_fk=1, type='French Vanilla', roast='dark', description='Strong coffee flavor with a hint of vanilla';
INSERT INTO product SET companyID_fk=1, type='Pumpkin spice', roast='medium', description='Seasonal';
INSERT INTO product SET companyID_fk=2, type='Dip Into Decaf', roast='light', description='You will barely notice it\'s coffee!';
Right now, our product entry form is a free text entry. It looks like this:
Company: <input type="text" name="company"/><br/>
We want it to only show approved companies from our DB, so looks like this:
Company: <select name="company">
<option value="2">Bean Traders</option>
<option value="3">Mean Beans</option>
<option value="1">Starbucks</option>
</select>
How can we do that? We can use SQL to ask the database for information:
SELECT companyID, name FROM company ORDER BY name;
Then use a while loop to add this to our page
$sql='SELECT companyID, name FROM company ORDER BY name';
$result = mysqli_query($link, $sql);
if (!$result) {
$error = 'Error fetching data: ' . mysqli_error($link);
echo $error;
exit();
}
while($recording=mysqli_fetch_array($result)){
//Some awesome PHP code here
}
Your sample code has a file called add-products.php. It is almost complete. Add code to the commented sections to get it to work.
Get stuck? Try looking at view-products.php to see how the while loop works there.
Photo credit: Mykl Roventine cc
Up until now, we have only passed data via forms. We can also pass data in the URL itself. This is called a parameter.
http://www.website.com/file.php?parameter=value
You can retrieve a parameter's value and store it as a variable
If you use a URL like this:
http://www.website.com/file.php?parameter=value
You can retrieve the values like this:
$variableName = htmlspecialchars($_GET["parameter"]);
How can we use this? On the view products page, I have set up a delete link for each item.
This link uses a parameter to send the product ID to the delete page.
Your sample code has a file called delete-products.php. It is almost complete. Add code to the commented sections to get it to work.
Let's combine queries, forms, parameters, and logic all into one. We will
On the view products page, I have set up an edit link for each item.
This link uses a parameter to send the product ID to the edit page.
In the file edit-products.php, we will:
SELECT fields FROM tablename WHERE field='value'
In the file product_edit_result.php, we will:
Your sample code has a file called edit-products.php and one called product_edit_result.php. They are almost complete. Add code to the commented sections to get it to work.