Commit b4940f84 authored by Sorrel Harriet's avatar Sorrel Harriet

bug fixing in lec 5 example

parent b1b819e7
<h3>Find a record!</h3>
<p>Enter an artist name or record title.</p>
<form action="results.php" method="GET">
<p>Enter an artist's name.</p>
<form action="?" method="GET">
<input type="hidden" name="page" value="results"/>
<fieldset>
<label for="artist_name">Artist name</label>
<label for="artist_name">Artist name:</label>
<input type="text" name="artist_name" />
<label for="record_title">Title</label>
<input type="text" name="record_title" />
</fieldset>
<button type="submit">Submit</button>
<button type="submit">Search</button>
</form>
......@@ -2,36 +2,28 @@
$content = "<h1>Records Found</h1>";
// check a search parameter was set
if (!isset($_GET['artist_name']) and !isset($_GET['record_title'])) { // neither parameter was set
// check if search parameter set
if (!isset($_GET['artist_name'])) {
$content .= "<p>Sorry, I don't know what you're looking for.</p>";
} else { // a parameter was set
} else { // search parameter was set
// check which parameter was set
if (isset($_GET['artist_name'])) { // artist_name was set
// get the name from the query string
$name = $_GET['artist_name'];
// split the name into an array of words
$parts = explode(" ", $name);
// define query
$sql = "SELECT r.title, r.price, a.first_name, a.last_name
FROM record r
JOIN artist a
ON r.artist_id=a.id
WHERE a.first_name='".$parts[0]."'
OR a.first_name='".$parts[1]."'
OR a.last_name='".$parts[0]."'
OR a.last_name='".$parts[1]."'";
// get the name from the query string
$name = $_GET['artist_name'];
// split the name into an array of words
$parts = explode(" ", $name);
// define query
$sql = "SELECT r.title, r.price, a.first_name, a.last_name
FROM record r
JOIN artist a
ON r.artist_id=a.id
WHERE a.first_name=".$parts[0];
} else if (isset($_GET['record_title'])) { // record_title was set
$title = $_GET['record_title'];
$sql = "SELECT r.title, r.price, a.first_name, a.last_name
FROM record r
JOIN artist a
ON r.artist_id=a.id
WHERE r.title=".$title;
}
$result = mysqli_query($link, $sql);
......@@ -40,29 +32,38 @@ if (!isset($_GET['artist_name']) and !isset($_GET['record_title'])) { // neither
echo mysqli_error($link);
} else {
$content .= "<table border='1'><tbody>";
// fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
// append table rows to $content string
$content .= "<tr>";
$content .= "<td>".$row['title']."</td>";
$content .= "<td>".$row['first_name']."</td>";
$content .= "<td>".$row['artist_name']."</td>";
$content .= "<td>".$row['year']."</td>";
$content .= "<td>&pound;".$row['price']."</td>";
$content .= "</tr>";
}
// append $content string with closing table tags
$content .= "</tbody></table>";
// free result set
mysqli_free_result($result);
}
}
// get number of rows
$num_rows = mysqli_num_rows($result);
// check some rows were found
if ($num_rows == 0) { // no rows found
$content = "<p>Sorry, no results found matching your query.</p>";
} else { // some rows were found
$content .= "<table border='1'><tbody>";
// fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
// append table rows to $content string
$content .= "<tr>";
$content .= "<td>".$row['title']."</td>";
$content .= "<td>".$row['first_name']."</td>";
$content .= "<td>".$row['last_name']."</td>";
$content .= "<td>&pound;".$row['price']."</td>";
$content .= "</tr>";
}
// append $content string with closing table tags
$content .= "</tbody></table>";
// free result set
mysqli_free_result($result);
}
}
}
// output the content HTML
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment