Please I need help in this question. I am new to connect php to mysql database.
ID: 3702857 • Letter: P
Question
Please I need help in this question. I am new to connect php to mysql database. the homework is t...
please I need help in this question. I am new to connect php to mysql database.
then when the user click on the one of the title it should appears on the URL the film_id of this title
I tried to write the code but I am not sure about it. please can some on check it for me.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSCI 5333 DBMS Spring 2018 Homework #6</title>
</head>
<?php
// Minimally documented.
include('sakiladb.php');
// Return the full URL, including the query string of the current script.
function selfURL() { // Assume http only
$selfURL = "http://" . $_SERVER["SERVER_NAME"];
if ($_SERVER["SERVER_PORT"] != "80") {
$selfURL .= ":".$_SERVER["SERVER_PORT"];
}
$selfURL .= $_SERVER["REQUEST_URI"];
return $selfURL;
}
if(!isset($_POST['submit']))
{
header("Location:index.php");
}
$submit_sql=" select title from film where name like '%" .$_POST[
'submit'] ."%';
$submit_quary=mysql_quary($submit_sql);
if(mysql_num_rows($submit_quary)!=0){
$submit_rs= mysql_fetch_assoc($submit_quary);
}
?>
if ($stmt = $mysqli->prepare($submit_sql)) {
$stmt->execute();
$stmt->bind_result($title);
$stmt->store_result();
$numResult = $stmt->num_rows;
}
if ($numResult == 0) {
$result .= <<<__NO_RESULT
Sorry, the databas is down. Please check later.
__NO_RESULT;
}
else {
$result .= "<h2>Search results for keyword:</h2> ";
$result .= "<tr><td>title</td></tr> ";
$selfURL = selfURL();
// Remove query string in the URL.
$selfURL = substr( $selfURL, 0, strrpos( $selfURL, "?"));
while ($stmt->fetch()) {
$result .= <<<__A_CATEGORY
<tr><td>$title</td><td><a href="$selfURL?catid=$catId">$title</a></td></tr>
__A_CATEGORY;
}
}
$stmt->free_result();
return $result;
} // categoryLinks
<?php
if(mysql_num_rows($submit_quary)!=0){
do{?>
<p href="$selfURL?filmid=$fId"><?php echo $submit_rs['title']; ?></p>
<?php
}while($submit_rs=mysql_fetch_assoc($submit_quary));
}
else
{
echo "No results found";
}
?>
// Get the film titles, length and actor counts, actors names
$query = <<<__QUERY
select distinct f.title, f.length, count(fa.actor_id)
as count, concat(a.first_name, a.lastname)
from film f,film_actor fa, actor a
where f.film_id=?
group by f.title, f.length
order by f.title, f.length
__QUERY;
if ($stmt = $mysqli->prepare($query))
{ $stmt->bind_param('i', $fid);
$stmt->execute();
$stmt->bind_result($film_id, $film_title, $actor_count, $actor_name);
$stmt->store_result();
$numResult = $stmt->num_rows;
}
if ($numResult == 0) {
$result .= <<<__NO_RESULT
Sorry, the databas is down. Please check later.
__NO_RESULT;
}
else
{
$result .= "<h2>search result for the keyword life:</h2> <ul> ";
$selfURL = selfURL();
// Remove query string in the URL.
if (strrpos($selfURL, "?"))
{
$selfURL = substr($selfURL, 0, strrpos($selfURL, "?"));
}
while ($stmt->fetch()) {
$result .= <<<__ONE_CATEGORY
<li><a href="$selfURL?catid=$catId">$catName</a></li>
__ONE_CATEGORY;
}
$result .= "</ul> ";
}
$stmt->free_result();
return $result;
}
</html>
Explanation / Answer
Please ask in comments, if something more need to be clarified. Thanks!