forked from b2much/gdi-intro-php-mysql
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass3-codesample.txt
90 lines (80 loc) · 2.82 KB
/
class3-codesample.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
***Connecting to a database***
If needed, replace ('localhost', 'root', '') with your server, username, and password. Place this in a new file called db.inc.php in the same folder as index.php
<?php
$link=mysqli_connect ('localhost', 'root', '');
if (!$link){
$output='Unable to connect to the database';
echo $output;
exit();
}
if (!mysqli_set_charset($link,'utf8')) {
$output = 'Unable to set database connection encoding.';
echo $output;
exit();
}
if (!mysqli_select_db($link, 'coffee')){
$output = 'Unable to locate the database.';
echo $output;
exit();
}
?>
----
***Form Code***
Drop this directly into the <body> section of your index.php file.
<form action="product_insert_result.php" method="get">
Company: <input type="text" name="company"/><br/>
Type: <input type="text" name="type"/><br/>
Roast:
<input type="radio" name="roast" value="light">Light</input>
<input type="radio" name="roast" value="medium">Medium </input>
<input type="radio" name="roast" value="dark">Dark</input>
<br/>
<textarea name="description" rows="10" cols="40"></textarea><br/>
<input type="submit" value="Submit"/>
</form>
----
***Form Processing Code***
Create a new file called product_insert_result.php and place it in the same folder as index.php
<?php
include 'db.inc.php';
$company = mysqli_real_escape_string($link, $_GET['company']);
$type = mysqli_real_escape_string($link, $_GET['type']);
$roast = mysqli_real_escape_string($link, $_GET['roast']);
$description = mysqli_real_escape_string($link, $_GET['description']);
$sql = "INSERT INTO product SET
company='$company',
type='$type',
roast='$roast',
description='$description'";
if (!mysqli_query($link, $sql)){
$error = 'Error adding submitted data: ' . mysqli_error($link);
echo $error;
exit();
}
header('Location:product_show.php');
?>
----
***Display Data***
Create a new file called product_show.php and place it in the same folder as index.php
<?php
include 'db.inc.php';
$sql='SELECT productID , company, type, roast, description FROM product ORDER BY productID DESC';
$result = mysqli_query($link, $sql);
if (!$result) {
$error = 'Error fetching data: ' . mysqli_error($link);
echo $error;
exit();
}
while($recording=mysqli_fetch_array($result)){
$id=htmlspecialchars($recording['productID'], ENT_QUOTES, 'UTF-8');
$company= htmlspecialchars($recording['company'], ENT_QUOTES, 'UTF-8');
$type=htmlspecialchars($recording['type'], ENT_QUOTES, 'UTF-8');
$roast=htmlspecialchars($recording['roast'], ENT_QUOTES, 'UTF-8');
$description=htmlspecialchars($recording['description'], ENT_QUOTES, 'UTF-8');
echo $company . ' ';
echo $type . ' ';
echo $roast . ' ';
echo $description . '<br />';
}
echo 'Return to <a href="index.php">home</a>.'
?>