litosgonzalez Posted August 9, 2012 Share Posted August 9, 2012 Hi guys, I'm nuvee with php and mysql, I'm writing the code to search and query data from Mysql, but something is wrong with the connection or with the code.... This is my search code search.html <!DOCTYPE html> <html> <head> <title></title> </head> <body> <form action="results.php" method="post"> Choose Search Type: <br/> <select name="searchtype"> <option value="object_id">Object ID</option> </select> <br/> Enter Search Term:<br/> <input name="searchterm" type="text"> <br/> <input type="submit" value="Search"> </form> </body> </html> ======== and this is my results.php sript. <!DOCTYPE html> <html> <head> <title></title> </head> <body> <?php //create short variables names $searchtype=$HTTP_POST_VARS['searchtype']; $searchterm=$HTTP_POST_VARS['searchterm']; $searchterm = trim($searchterm); if (!searchtype || !$searchterm) { echo 'You have not entered search detail. Please go back and try again.'; exit; } $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); @ $db = mysql_pconnect('localhost', 'root mysql'); if (!$db) { echo 'Error: Could not connect to database. Please try again later.'; exit; } mysql_select_db('products'); $query = "select * from objects where ".$searchtype." like '%".$searchterm."%'"; $result = mysql_query($query); $num_results = mysql_num_row($result); echo '<p>Number of objects found: '.$num_results.'</p>'; for ($i=0; $i<$num_results; $i++) { $row = mysql_fetch_array($result); echo '<p><strong>'.($i+1).'. Object ID: '; echo htmlspecialchars(stripslashes($row['object_id'])); echo '</p>'; } ?> </body> </html> thank for your help....!!! Quote Link to comment https://forums.phpfreaks.com/topic/266858-querying-a-database-from-the-web/ Share on other sites More sharing options...
peipst9lker Posted August 9, 2012 Share Posted August 9, 2012 1. Use $_POST['searchtype'] instead of $HTTP_POST_VARS['searchtype']; 2. Your mysq_pconnect() has wrong syntax check here for right syntax. Quote Link to comment https://forums.phpfreaks.com/topic/266858-querying-a-database-from-the-web/#findComment-1368083 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.