Low cost ecommerce web development India
flash website design
In this chapter, you learned some new PHP functions that allow you to interface
with a MySQL database server. Using these functions, you built your first database-
driven Website, which published the jokes database online, and allowed
visitors to add jokes of their own to it.
In Chapter 5, we go back to the MySQL command line. We'll learn how to use
relational database principles and advanced SQL queries to represent more
complex types of information, and give our visitors credit for the jokes they
add!
“Homework” Solution
Here's the solution to the "homework" challenge posed above. These
changes
were required to insert a "Delete this Joke" link next to each joke:
. Previously, we passed an $addjoke variable with our "Add a Joke!"
link at the
bottom of the page to signal that our script should display the joke entry form,
80
Publishing MySQL Data on the Web
instead of the usual list of jokes. In a similar fashion, we pass a deletejoke
variable with our "Delete this Joke" link to indicate our desire to
have a joke
deleted.
. For each joke, we fetch the ID column from the database, along with the
JokeText column, so that we know which ID is associated with each joke in
the database.
. We set the value of the $_GET['deletejoke'] variable to the ID of the joke
that we're deleting. To do this, we insert the ID value fetched from the database
into the HTML code for the "Delete this Joke" link of each joke.
. Using an if statement, we watch to see if $_GET['deletejoke'] is set to a
particular value (through the isset function) when the page loads. If it is,
we use the value to which it is set (the ID of the joke to be deleted) in an
SQL
DELETE statement that deletes the joke in question.
Here's the complete code, which is also available as challege.php in the code
archive. If you have any questions, don't hesitate to post them in the SitePoint
Forumsi!
<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($_GET['addjoke'])): // If the user wants to add a joke
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40"
wrap>
</textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT"
/>
</p>
</form>
<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root', 'mypasswd');
if (!$dbcnx) {
ihttp://www.sitepointforums.com/
81
“Homework” Solution
die( '<p>Unable to connect to the ' .
'database server at this time.</p>' );
}
// Select the jokes database
if (! @mysql_select_db('jokes') ) {
die( '<p>Unable to locate the joke ' .
'database at this time.</p>' );
}
// If a joke has been submitted,
// add it to the database.
if (isset($_POST['submitjoke'])) {
$joketext = $_POST['joketext'];
$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";
if (@mysql_query($sql)) {
echo('<p>Your joke has been added.</p>');
} else {
echo('<p>Error adding submitted joke: ' .
mysql_error() . '</p>');
}
}
// If a joke has been deleted,
// remove it from the database.
if (isset($_GET['deletejoke'])) {
$jokeid = $_GET['deletejoke'];
$sql = 'DELETE FROM Jokes
WHERE ID=$jokeid';
if (@mysql_query($sql)) {
echo('<p>The joke has been deleted.</p>');
} else {
echo('<p>Error deleting joke: ' .
mysql_error() . '</p>');
}
}
echo('<p> Here are all the jokes in our database: </p>');
// Request the ID and text of all the jokes
$result = @mysql_query('SELECT ID, JokeText FROM Jokes');
if (!$result) {
die('<p>Error performing query: ' .
mysql_error() . '</p>');
82
Publishing MySQL Data on the Web
}
// Display the text of each joke in a paragraph
// with a "Delete this Joke" link next to each.
while ( $row = mysql_fetch_array($result) ) {
$jokeid = $row['ID'];
$joketext = $row['JokeText'];
echo('<p>' . $joketext .
'<a href="' . $_SERVER['PHP_SELF'] .
'?deletejoke=' . $jokeid . '">' .
'Delete this Joke</a></p>');
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo('<p><a href="' . $_SERVER['PHP_SELF'] .
'?addjoke=1">Add a Joke!</a></p>');
endif;
?>
</body>
</html>
website designer freelance ASP PHP ecommerce web developer
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110