Jump to content

Can you help?


Catling

Recommended Posts

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

Link to comment
Share on other sites

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 == "")

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :D

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.