...

View Full Version : php search database



kimmyko
05-17-2011, 02:45 AM
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

shadowmaniac
05-17-2011, 05:01 AM
Read the rules for posting code...

$HTTP_POST_VARS is deprecated and you should be using $_POST instead.
Your code should look something like:


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.

kimmyko
05-17-2011, 02:30 PM
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>

abduraooft
05-17-2011, 02:49 PM
@ $db = mysql_pconnect('localhost','bookorama','bookarama123');
Do you really need a persistent connection (http://php.net/manual/en/function.mysql-pconnect.php#refsect1-function.mysql-pconnect-notes)?

Anyway, change the above line to

$db = mysql_pconnect('localhost','bookorama','bookarama123') or die(mysql_error()); and check the output.

kimmyko
05-17-2011, 05:33 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum