473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Having problems implementing search

Hi there, I'm trying to create a simple DVD shopping cart. I have
implemented a way to browse by genre and am trying to implement a way to
search by selecting title, director and actor and typing some text in the
text field for it to search the database.

As trying to implement the title, director and actor searches seemed to
complicated to try together, I am trying to make the title one work first
and then move on to the other two from there (2 more if statements
hopefully). Any help or advice would be great.

When I try to search with the code below, it just brings up all of the DVDs
again (I'm also not sure why the whole list of DVDs appears when the page is
first loaded).

Here is my code:

<?php

// this section creates a session, connects to the db and handles the two
buttons being pressed.
session_start ();

require ('mysql.php');
mysql_connect ($host, $user, $passwd);
mysql_select_db ($dbName);

if (isset ($_GET['add']))
$_SESSION['trolley'][] = $_GET['add'];
else if ($_GET['op'] === 'clear')
$_SESSION['trolley'] = "";
?>

<html>
<head>
<title>PHP & MySQL</title>
</head>
<center>
<body><P>
</P>
<form action="browse_search.php" method=get>

<select name="search_in">
<option value="title">title</option>
<option value="director">director</option>
<option value="actor">actor</option>
</select>

<input type="text" size="16" maxlength="24" name="search_text" />

<input type="submit"/>
</form

</center>
<table>
<?php

$getdvds_query = "";
//this section gets a query from the db using genre selected by the user,
and outputs DVDs based
//on selected genre
$getgenres_query = mysql_query ('SELECT * FROM genre');

while ($genre = mysql_fetch_assoc ($getgenres_query))
echo "<a href=\"$PHP_SELF?genrename=$genre[name]\">$genre[name]</a><BR>";

echo "<BR>";

if ($search_in == "title") {
$getdvds_query = mysql_query ("SELECT title, duration, rel, descr, genre,
stock, price FROM dvd where title LIKE '%$search_text%'");
}

$getdvds_query = mysql_query ("SELECT title, duration, rel, descr, genre,
stock, price FROM dvd where genre LIKE '%$genrename%'");

echo "<table bgcolor=\"ddeeff\"><thead><tr>";
for ( $i = 0 ; $i < mysql_num_fields($getdvds_query) ; $i++ ) {
echo "<th bgcolor=\"abcdef\">" . mysql_field_name($getdvds_query,$i) .
"</th>\n";
}

if (mysql_num_rows ($getdvds_query))
{
for ($id = 0; $dvd[] = mysql_fetch_assoc ($getdvds_query); $id++)
{
echo "<tr>\n";
echo "<td>{$dvd[$id][title]}</td>\n";
echo "<td>{$dvd[$id][duration]}</td>\n";
echo "<td>{$dvd[$id][rel]}</td>\n";
echo "<td>{$dvd[$id][descr]}</td>\n";
echo "<td>{$dvd[$id][genre]}</td>\n";
echo "<td>{$dvd[$id][stock]}</td>\n";
echo "<td>{$dvd[$id][price]}</td>\n";

echo "<td><a href=\"$PHP_SELF?add=$id\">add to basket</a></td>";
echo "</tr>\n";
}
}

?>
</table>
<a href="browse_search.php?op=clear">empty trolley</a><br /><br />
<?php
//this section outputs basket contents
if (!empty ($_SESSION['trolley']))
{
echo 'Trolley contents:<br />';

foreach ($_SESSION['trolley'] as $trolley_item)
echo $dvd[$trolley_item]['title']."<br />";
}
else
echo 'Trolley is empty!';
?>

</body>
</html>
Jul 17 '05 #1
2 1689
Here is my code to search for a director and a actor:

else if ($search_in == "director") {
$getdvds_query = mysql_query ("SELECT * FROM dvd WHERE dirid = (SELECT dirid
FROM director WHERE name LIKE '%$search_text%')");
}
else if ($search_in == "actor") {
$getdvds_query = mysql_query ("SELECT d.title, d.descr, d.genre, actd.name,
actd.descr FROM dvd d, actordvd actd WHERE dvdid = (SELECT dvdid FROM
actordvd WHERE actorid = (SELECT actorid FROM actor WHERE name LIKE
'%$search_text%')");
}

But when I use it I get the following errors:
Warning: mysql_num_fields(): supplied argument is not a valid MySQL result
resource in /home/yj103/public_html/browse_search.php on line 65

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result
resource in /home/yj103/public_html/browse_search.php on line 69
Jul 17 '05 #2

"James" <no************@yahoo.com>
wrote in news:bv**********@hercules.btinternet.com...
But when I use it I get the following errors:
Warning: mysql_num_fields(): supplied argument is not a valid MySQL result
resource in /home/yj103/public_html/browse_search.php on line 65
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result
resource in /home/yj103/public_html/browse_search.php on line 69


Usually i do mysql results something like this:

// for select statements
$result = mysql_query("...");
if(!$result)
die("No result, error:".mysql_error());

if(!mysql_num_rows($result))
die("No rows");
while($row = mysql_fetch_assoc($result));
{
....
}

I didnt check but you get the idea.
For insert and update statement use mysql_affected_rows.


Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Troels F. Smit | last post by:
Where can I find information on implementing a search function on my site ? -- Mvh. Troels F. Smit
2
by: Dmitri Zhukov | last post by:
Hello everyone, I am implementing software which has a medium sized number of text strings (max 100K) which are represented in a listbox. I want user to be able to search the string by typing...
19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
2
by: StevePBurgess | last post by:
I have a website that authenticates users and then allows them to visit member only parts of the site. The authentication uses cookies. When the user has logged in and the script has satisfied...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
2
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in...
3
by: 9966 | last post by:
Well previously I've asked a lot of question regarding Travelling Salesman Problem and I got a lot of helpful feedback from here. Thanks again for helping me. Now I've completed the full code....
11
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I have worked with application settings in VS2005 and C# for awhile, but usually with standard types. I have been trying to store a custom container/class/type in an application setting and I have...
2
by: Bart Kastermans | last post by:
Summary: can't verify big O claim, how to properly time this? On Jun 15, 2:34 pm, "Terry Reedy" <tjre...@udel.eduwrote: Thanks for the idea. I would expect the separation to lead to somewhat...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.