Having problems implementing search

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    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">t itle</option>
    <option value="director ">director</option>
    <option value="actor">a ctor</option>
    </select>

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

    <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_quer y = mysql_query ('SELECT * FROM genre');

    while ($genre = mysql_fetch_ass oc ($getgenres_que ry))
    echo "<a href=\"$PHP_SEL F?genrename=$ge nre[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=\"ddeef f\"><thead><tr> ";
    for ( $i = 0 ; $i < mysql_num_field s($getdvds_quer y) ; $i++ ) {
    echo "<th bgcolor=\"abcde f\">" . mysql_field_nam e($getdvds_quer y,$i) .
    "</th>\n";
    }

    if (mysql_num_rows ($getdvds_query ))
    {
    for ($id = 0; $dvd[] = mysql_fetch_ass oc ($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_SEL F?add=$id\">add to basket</a></td>";
    echo "</tr>\n";
    }
    }

    ?>
    </table>
    <a href="browse_se arch.php?op=cle ar">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>


  • James

    #2
    Re: Having problems implementing search

    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_field s(): supplied argument is not a valid MySQL result
    resource in /home/yj103/public_html/browse_search.p hp on line 65

    Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result
    resource in /home/yj103/public_html/browse_search.p hp on line 69


    Comment

    • Perttu Pulkkinen

      #3
      Re: Having problems implementing search


      "James" <nospamheredude @yahoo.com>
      wrote in news:bvokke$aln $1@hercules.bti nternet.com...
      [color=blue]
      > But when I use it I get the following errors:
      > Warning: mysql_num_field s(): supplied argument is not a valid MySQL result
      > resource in /home/yj103/public_html/browse_search.p hp on line 65
      > Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result
      > resource in /home/yj103/public_html/browse_search.p hp on line 69[/color]

      Usually i do mysql results something like this:

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

      if(!mysql_num_r ows($result))
      die("No rows");
      while($row = mysql_fetch_ass oc($result));
      {
      ....
      }

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




      Comment

      Working...