Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php search database

    hi i am trying to make search for items in my database but its not wokring, here is the cod ehtat i have so far


    <html>
    <head>
    <title> Media-O RAMA Catalog Search</title>
    </head>

    <body
    <H1>Media-O RAMA Catalog Search</H1>
    <?php
    // create short variable names
    $searchtype=$HTTP_POST_VARS['searchtype'];
    $searchterm=$HTTP_POST_VARS['searchterm'];
    $searchterm= trim($searchterm) ;

    if (!$searchtype || !$searchterm)
    {
    echo 'You have not entered search details. Please go back and try again';
    exit;
    }

    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchtem);

    @ $db = mysql_pconnect('localhost','bookorama','bookarama123');

    if (!$db)
    {
    echo 'Error: Could not connect to database. Please go back and try again';
    exit;
    }

    mysql_select_db('booksr');
    $query = "select * from booksr where ".$searchtype." like '%".$searchterm."%'";
    $result = mysql_query($query);

    $num_results = mysql_num_rows_($result);

    echo '<p>Number of books found: '.$num_result.'</p>';

    for ($i=0; $i <$num_results; $i++);
    {
    $row = mysql_fetch_array($result);
    echo '<p><strong>'. ($i+1).'.Title; ';
    echo htmlspecialchars(stripslashes($row['title']));
    echo '</strong><br />Author: ';
    echo stripslashes($row['author']);
    echo '<br />ISBN: ';
    echo stripslashes($row['ISBN']);
    echo '<br />Price: ';
    echo stripslashes($row['price']);
    echo '</p>';
    }
    ?>

    </body>
    </html>


    it keeps givng me this error code
    Media-O RAMA Catalog Search
    Notice: Undefined variable: HTTP_POST_VARS in G:\webclient\xampp\htdocs\web\results.php on line 10

    Notice: Undefined variable: HTTP_POST_VARS in G:\webclient\xampp\htdocs\web\results.php on line 11
    You have not entered search details. Please go back and try again

    any help would be much appreciated

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Read the rules for posting code...

    $HTTP_POST_VARS is deprecated and you should be using $_POST instead.
    Your code should look something like:
    PHP Code:
    if(isset($_POST['searchtype']))
    {
        
    $searchtype=$_POST['searchtype'];

    Check your form submit page and ensure that it's of type "POST" and double-check the variable names as well.

  • #3
    New Coder
    Join Date
    May 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    connection error

    thanks for that, but when i edited it, it is now telling me its unable to connect to database
    Media-O RAMA Catalog Search Error: Could not connect to database. Please go back and try again, where am i going wrong.

    <?php
    // create short variable names
    $searchtype=$_POST['searchtype'];
    $searchterm=$_POST['searchterm'];
    $searchterm= trim($searchterm) ;

    if (isset($_POST['searchtype']))
    {
    $searchtype=$_POST['searchtype'];
    }

    $searchtype = addslashes($searchtype);


    @ $db = mysql_pconnect('localhost','bookorama','bookarama123');

    if (!$db)
    {
    echo 'Error: Could not connect to database. Please go back and try again';
    exit;
    }

    mysql_select_db('booksr');
    $query = "select * from booksr where ".$searchtype." like '%".$searchterm."%'";
    $result = mysql_query($query);

    $num_results = mysql_num_rows_($result);

    echo '<p>Number of books found: '.$num_result.'</p>';

    for ($i=0; $i <$num_results; $i++);
    {
    $row = mysql_fetch_array($result);
    echo '<p><strong>'. ($i+1).'.Title; ';
    echo htmlspecialchars(stripslashes($row['title']));
    echo '</strong><br />Author: ';
    echo stripslashes($row['author']);
    echo '<br />ISBN: ';
    echo stripslashes($row['ISBN']);
    echo '<br />Price: ';
    echo stripslashes($row['price']);
    echo '</p>';
    }
    ?>

    </body>
    </html>

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    @ $db = mysql_pconnect('localhost','bookorama','bookarama123');
    Do you really need a persistent connection?

    Anyway, change the above line to
    PHP Code:
    $db mysql_pconnect('localhost','bookorama','bookarama123') or die(mysql_error()); 
    and check the output.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New Coder
    Join Date
    May 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    new error

    that did not work as it gave me a new error message

    Media-O RAMA Catalog Search
    Notice: Undefined index: searchtype in G:\webclient\xampp\htdocs\web\results.php on line 10

    Notice: Undefined index: searchterm in G:\webclient\xampp\htdocs\web\results.php on line 11
    Access denied for user 'bookorama'@'localhost' (using password: YES)

    $searchtype=$_POST['searchtype']; line 10
    $searchterm=$_POST['searchterm']; line 11


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •