Ответов: 7 Просмотров: 14998
|
Доброго времени суток!
Я недавно начал изучать php, и возникла проблема с написанием кода.
Вообщем у меня есть база с инфой о фильмах (название, режиcсер, время фильма и год).
Я хочу сделать поиск по этой базе, но у меня почему-то из формы для запроса ничего не передается.
Вот форма:
CODE ( html):
скопировать код в буфер обмена
<html> <head> <title>Search</title> </head> <body> <center> Enter the original title, director's name, duration or year of the film you are looking for: <form action="search.php" method="post" onsubmit="javascript: if ((search_query.value=='')||(search_query.value=='Поиск...')) { return false; } else { return true; }"> <input type="text" name="search_query" onfocus="if (this.value=='Поиск...') this.value=''" value="Поиск..." style="font: 12px Verdana; height:20px; width:200px;" /> <input type="submit" name="name" value="Найти" style="font: bold 12px Verdana; background-color:#FFFFFF; height:20px; width:50px;"/> </form> </center> </body> </html>
вот search.php:
PHP:
скопировать код в буфер обмена
<html> <head> <title>Search Results</title> </head> <body> <?PHP global $search_query; if(isset($_POST['search_query'])){$search_query = $_POST['search_query'];} $query= mysql_query("SELECT * FROM films WHERE title = '$search_query' OR director = '$search_query' OR duration = '$search_query' OR year = '$search_query' ORDER BY year DESC"); if ($result == 0) { echo "Sorry, couldn't find anything about your query ($search_query)"; } else if ($result == 1) { echo "Found <b>1</b> match!<br>"; } else { echo "Found <b>$result</b> matches! <br>"; } { $title= $row["title"]; $director = $row["director"]; $duration = $row["duration"]; $year= $row["year"]; echo "The original title of the film is: $title.<br>"; echo "The director's name of the film(s) is: $director.<br>"; echo "The duration of the film(s) is: $duration minutes. <br>"; echo "The year of the film(s) is: $year.<br>"; } ?> </body> </html>
когда я что-то ввожу в форму и жму "найти", в браузере вылезает следующее:
Цитата:1 match!
"; } else { echo "Found $result matches!
"; } while ($row= mysql_fetch_array($query)) { $title= $row["title"]; $director = $row["director"]; $duration = $row["duration"]; $year= $row["year"]; echo "The original title of the film is: $title.
"; echo "The director's name of the film(s) is: $director.
"; echo "The duration of the film(s) is: $duration minutes.
"; echo "The year of the film(s) is: $year.
"; } ?>
это пишется независимо от того, есть ли это в базе, один там результат, несколько или вообще ничего.
когда просто запускаю search.php все работает, на монитор выводится:
Цитата:Sorry, couldn't find anything about your query () , т.к. ничего не запрошено,
или я могу, например, в ручную установить значение, заменив 12 строчку в search.php на:
, запустив его, мне вылезет:
Цитата:Found 2 matches!
The original title of the film is: Inception.
The director's name of the film(s) is: Nolan.
The duration of the film(s) is: 148 minutes.
The year of the film(s) is: 2010.
The original title of the film is: The Town.
The director's name of the film(s) is: Affleck.
The duration of the film(s) is: 124 minutes.
The year of the film(s) is: 2010.
как я и хочу, то что есть в базе.
Насколько я понимаю, проблема в том, что из формы отправки не передаются значения, а значит проблема в ней. Или передаются, но у меня никак не получается их вытащить из POST, т.е. я как-то неправильно пишу 12ю строчку в search.php.
Пожалуйста, помогите разобраться, сам никак не могу найти свою ошибку.
Заранее благодарен. |