Catling Posted April 3, 2008 Share Posted April 3, 2008 I am having trouble with my script. I am trying to make a search for a particular actor that is typed into my text area. e.g like searching hugh grant on imdb.com. Im trying to read in the input from the drop down search term and the search bar. My Problem: My script will execute but it wont go past the point in my code (Highlighted the point in bold below) that tests to see if I have selected a search type from the drop down menu and entered a string in the searchterm text box. It simply just echos "Please enter search details. Please go back and try again". I just can not understand why it wont read in my input??????? heres my script: <html> <body> <?php //create short variable names $searchtype=$HTTP_POST_VARS['searchtype']; $searchterm=$HTTP_POST_VARS['searchterm']; // cut out the whitespace $searchterm= trim($searchterm); // testing to see the user entered a search type and a search term if (!$searchtype || !$searchterm) { echo 'Please enter search details. Please go back and try again'; exit; } // slashes out control characters $searchtype= addslashes($searchtype); $searchterm= addslashes($searchterm); // make connection to database $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; @ $db = mysql_pconnect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); if (!$db) { echo 'Error: Could not connect to Database. Please Try Again'; exit; } $db=mysql_select_db('moviedatabase') or die("could not connect ".mysql_error); $query = "SELECT * FROM moviedatabase WHERE ".$searchtype." LIKE '%".$searchterm."%'"; $result = mysql_query($query); $num_results = mysql_num_rows($result); echo '<p> Number of Results found: '.$num_results.' </p>'; // loop to output number of results to the screen for ($i=0; $i <$num_results; $i++) { // test to see that the search type is Actor/Actress if ($searchtype == "Actor/Actress") { echo "<table border=1 >"; echo "<tr><td> Actor ID</td><td>Forename</td><td>Surname</td>><td>Age</td><td>Date of Birth</td><td>Nationality</td><td>Character Names</td><td>Photo</td></tr>"; while($row = MySQL_fetch_array($result)) { echo "<td>{$row['Actor_ID']}</td>"; echo "<td>{$row['Forename']}</td>"; echo "<td>{$row['Surname']}</td>"; echo "<td>{$row['Age']}</td>"; echo "<td>{$row['Date_of_Birth']}</td>"; echo "<td>{$row['Nationality']}</td>"; echo "<td>{$row['Character_Names']}</td>"; echo "<td>{$row['Photo']}</td></tr>"; } echo "</table>"; //else ( echo ' No results Found: Please go back and try again') } } ?> </body> </html> Any help would be much appreciated Quote Link to comment Share on other sites More sharing options...
almightyegg Posted April 3, 2008 Share Posted April 3, 2008 $searchtype = $_POST['searchtype']; $searchterm = $_POST['searchterm']; Quote Link to comment Share on other sites More sharing options...
unsider Posted April 3, 2008 Share Posted April 3, 2008 //create short variable names $searchtype=$_POST['searchtype']; $searchterm=$_POST['searchterm']; EDIT: oops, nevermind. Quote Link to comment Share on other sites More sharing options...
Catfish Posted April 3, 2008 Share Posted April 3, 2008 Being not so great with php and having a shocking memory, i'll just add that you might have to check for any typos with the variable names (especially in the file that calls your script). And I was also going to say you can try using if ($searchtype == "" || $searchterm == "") Quote Link to comment Share on other sites More sharing options...
stuffradio Posted April 3, 2008 Share Posted April 3, 2008 As the person a few posts above said... use $_POST instead of HTTP_POST_VARS. I'm pretty sure that's been deprecated for quite some time now Quote Link to comment Share on other sites More sharing options...
xnowandtheworldx Posted April 3, 2008 Share Posted April 3, 2008 It would probably be a lot better if you tested to see if the search terms, and whatever is selected using // testing to see the user entered a search type and a search term if (!isset($searchtype) || !isset($searchterm)) { echo 'Please enter search details. Please go back and try again'; exit; } it just absolutely makes sure that it IS empty. So I believe overall it is better to use that than just checking it like that, because from what I read a while back, even if it has a 0 value it can still pass as true sometimes. I'm not sure if it's fixed or anything, but i've used the isset() function ever since. Hope I made since haha. Quote Link to comment Share on other sites More sharing options...
Catling Posted April 3, 2008 Author Share Posted April 3, 2008 Thanks for your help guys. I think my book from the library is out of date I now get an error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\wamp\apps\phpmyadmin2.11.5\search.php on line 61(highlighted the line below) while($row = MySQL_fetch_array($result)) { echo "<td>{$row['Forename']}</td>"; echo "<td>{$row['Surname']}</td>"; echo "<td>{$row['Age']}</td>"; echo "<td>{$row['Date_of_Birth']}</td>"; echo "<td>{$row['Nationality']}</td>"; echo "<td>{$row['Character_Names']}</td>"; echo "<td>{$row['Photo']}</td></tr>"; } echo "</table>"; Any advice? Quote Link to comment Share on other sites More sharing options...
stuffradio Posted April 3, 2008 Share Posted April 3, 2008 What happens if you go echo "<td>{$row[Forename]}</td>"; ?? Also try mysql_fetch_array instead of Mysql_fetch_array. I think it might be case sensitive Quote Link to comment Share on other sites More sharing options...
Catling Posted April 3, 2008 Author Share Posted April 3, 2008 What happens if you go echo "<td>{$row[Forename]}</td>"; ?? Also try mysql_fetch_array instead of Mysql_fetch_array. I think it might be case sensitive wont it just output the whole thing <td>{$row[Forename]}</td>; as a string because its in quotations? Quote Link to comment Share on other sites More sharing options...
stuffradio Posted April 3, 2008 Share Posted April 3, 2008 Nope, not in my experience at least. Quote Link to comment Share on other sites More sharing options...
Catling Posted April 3, 2008 Author Share Posted April 3, 2008 What happens if you go echo "<td>{$row[Forename]}</td>"; ?? Also try mysql_fetch_array instead of Mysql_fetch_array. I think it might be case sensitive I've tried it and it just ignores the line and move onto the next line which produces the same error as the line above. Quote Link to comment Share on other sites More sharing options...
stuffradio Posted April 3, 2008 Share Posted April 3, 2008 Catling that's because now you need to do it for the rest of the rows. After you do that, it should work. Quote Link to comment Share on other sites More sharing options...
Catling Posted April 4, 2008 Author Share Posted April 4, 2008 Ive fixed that. Now i get: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\apps\phpmyadmin2.11.5\search.php on line 44 Line 44 is: $num_results = mysql_num_rows($result); I know mysql_num_rows() needs to be used with a SELECT statement, which I have done in my query. Im confused as to what it might be??? please help Quote Link to comment Share on other sites More sharing options...
stuffradio Posted April 4, 2008 Share Posted April 4, 2008 This is not correct. $query = "SELECT * FROM moviedatabase WHERE ".$searchtype." LIKE '%".$searchterm."%'"; $query = "SELECT * FROM `moviedatabase` WHERE '$searchtype' LIKE %$searchterm%"; Quote Link to comment Share on other sites More sharing options...
Catling Posted April 4, 2008 Author Share Posted April 4, 2008 it is movidedatabase? Because that is the name of my database. Im guessing I cant just look in the whole database and actually have to point to a particular table? Quote Link to comment Share on other sites More sharing options...
stuffradio Posted April 4, 2008 Share Posted April 4, 2008 if moviedatabase is the name of a database and not a table... you need to change it to the name of a table. The table is the name you need to change it to Quote Link to comment Share on other sites More sharing options...
Catling Posted April 4, 2008 Author Share Posted April 4, 2008 if moviedatabase is the name of a database and not a table... you need to change it to the name of a table. The table is the name you need to change it to I think I have a problem with my query. The syntax in my book must be different from the version of SQL im using, could you assist me with this query please? I think it must be something to do with single and double quotations $query = 'SELECT * FROM actor WHERE ".$searchtype." LIKE '%".$searchterm."%''; Any help would be much appreciated Quote Link to comment Share on other sites More sharing options...
stuffradio Posted April 4, 2008 Share Posted April 4, 2008 Already gave you the answer $query = "SELECT * FROM `actor` WHERE $searchtype LIKE '%$searchterm%'''; Quote Link to comment Share on other sites More sharing options...
Catling Posted April 4, 2008 Author Share Posted April 4, 2008 Already gave you the answer $query = "SELECT * FROM `actor` WHERE $searchtype LIKE '%$searchterm%'''; Thanks very much. I now have the following error again: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\apps\phpmyadmin2.11.5\search.php on line 44 I don't understand what this is? please help Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 4, 2008 Share Posted April 4, 2008 if the query is correct then no rows have been found - I often see this on windows and its not really an error (if the query syntax is ok). Quote Link to comment Share on other sites More sharing options...
Catling Posted April 4, 2008 Author Share Posted April 4, 2008 if the query is correct then no rows have been found - I often see this on windows and its not really an error (if the query syntax is ok). Well im choosing option Author/Actress from the menu bar on my website and then entering Hugh Grant in to my search text box. So im reading the menu bar in with variable $searchtype and reading the text box in with $searchterm. Just to note I have placed Hugh Grants record in my database So I think im selecting all from my actor table where read in $searchtype LIKE to read in a string $searchterm i.e $query = "SELECT * FROM `actor` WHERE $searchtype LIKE '%$searchterm%'''; where am i going wrong? please help Quote Link to comment Share on other sites More sharing options...
Catling Posted April 4, 2008 Author Share Posted April 4, 2008 can anyone help? Quote Link to comment 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.