Jump to content

PHP Search database problem


PHPnewby!

Recommended Posts

Hi guys, I'm having problems getting my search form to do anything! Any help would be GREATLY appreciated...think its more a problem with the PHP than SQL? Thanks.               

 

<form name="search" method="post" action="<?=$PHP_SELF?>">

                    <table width="100%" border="0">

                        <tr>

                          <td width="26%">Gender</td>

                          <td width="27%"><select name="Gender" id="Gender">

                            <option>Female</option>

                            <option>Male</option>

                          </select>

                          </td>

                          <td width="21%"> </td>

                          <td width="26%"> </td>

                        </tr>

                        <tr>

                          <td>Interests</td>

                          <td><input name="Interests" type="text" id="Interests" /></td>

                          <td> </td>

                          <td> </td>

                        </tr>

                        <tr>

                          <td>Year of Birth from:</td>

                          <td><input name="DOB_from" type="text" id="DOB_from" /></td>

                          <td>Year of Birth to: </td>

                          <td><input name="DOB_to" type="text" id="DOB_to" /></td>

                        </tr>

                        <tr>

                          <td>Travel Aspirations</td>

                          <td><input name="TravelAspirations" type="text" id="TravelAspirations" /></td>

                          <td> </td>

                          <td> </td>

                        </tr>

                        <tr>

                          <td>Message </td>

                          <td><input name="Message" type="text" id="Message" /></td>

                          <td> </td>

                          <td> </td>

                        </tr>

                      </table>

                      <p>

                        <input type="hidden" name="searching" value="yes" />

                        <input type="submit" name="search" value="Search" />

                          </p>

                    </form>

<?php

 

//Now we search for our search term, in the field the user specified

$Gender = $_POST['Gender'];

$Interests = $_POST['Interests'];

$DOB_from = $_POST['DOB_from'];

$DOB_to = $_POST['DOB_to'];

$TravelAspirations = $_POST['TravelAspirations'];

$Message = addslashes($_POST['Message']);

 

//This is only displayed if they have submitted the form

if ($_POST['searching']=="yes")

{

echo "<h2>Members who met your search criteria</h2><p>";

 

//If they did not enter a search term we give them an error

if ($Gender AND $Interests AND $Message AND $DOB_from AND $DOB_to AND $TravelAspirations = "")

{

echo "<p>You forgot to enter a search term";

exit;

}

 

$data = mysql_query("SELECT * FROM Members WHERE (Gender = '$Gender') OR (Interests LIKE '%$Interests%') OR (DOB BETWEEN '$DOB_from' AND '$DOB_to') OR  (TravelAspirations LIKE '%$TravelAspirations%') OR (Message LIKE '%$Message%')");

 

//And we display the results

while($result = mysql_fetch_array( $data ))

{

echo $result['Gender'];

echo "<br>";

echo $result['DOB'];

echo "<br>";

echo $result['Interests'];

echo "<br>";

echo $result['Interests'];

echo "<br>";

echo $result['TravelAspirations'];

echo "<br>";

echo $result['Message'];

echo "<br>";

echo "<br>";

}

 

$name = $result['username'];

$email = $result['email'];

echo "<a href='mailto:".$email. "'>".$name."</a>";

 

//This counts the number or results - and if there wasn't any it gives them a little message explaining that

$anymatches=mysql_num_rows($data);

if ($anymatches == 0)

{

echo "Sorry, but we can not find an entry to match your query<br><br>";

}

 

//And we remind them what they searched for

echo "<b>Searched For:</b> ";

}

?>

Link to comment
Share on other sites

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /export2/webroot/users/c3031698/ContactMembers.php on line 211

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /export2/webroot/users/c3031698/ContactMembers.php on line 234

 

It did once show a response when I selected a gender...but it showed both genders....and the response although I haven't changed anything is never to be seen again! :S

Link to comment
Share on other sites

Just glancing at it I found this error:

 

if ($Gender AND $Interests AND $Message AND $DOB_from AND $DOB_to AND $TravelAspirations = "")

 

Should be:

 

if ($Gender == ""  AND $Interests == "" AND $Message == "" AND $DOB_from = "" AND $DOB_to == "" AND $TravelAspirations == "")

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /export2/webroot/users/c3031698/ContactMembers.php on line 211

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /export2/webroot/users/c3031698/ContactMembers.php on line 234

 

Something is wrong with your queires

 

 

Link to comment
Share on other sites

try and get an error message from mysql

 

$sql = "SELECT * FROM Members WHERE (Gender = '$Gender') OR (Interests LIKE '%$Interests%') OR (DOB BETWEEN '$DOB_from' AND '$DOB_to') OR  (TravelAspirations LIKE '%$TravelAspirations%') OR (Message LIKE '%$Message%')";
mysql_query($sql) or die(mysql_error());

 

Ray

Link to comment
Share on other sites

Thanks Ray, I've done what you said.....it now states "No Database Selected" with no error messages...and it doesn't matter whether I have selected anything within the search form or not!

 

I tried re-stating the connect to database line within this section of php, but no change!

 

Any other ideas? sorry! Thanks.

Link to comment
Share on other sites

well you need to connect to your database. try this at the top.

 

<?php
// database settings
$dbhost = 'localhost';	// database server
$dbuser = 'username';      	// db username
$dbpass = 'password';    	// db password
$dbname = 'mydb';  // db name

// connect to database
$mysql_conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to Mysql, Please check settings and try again");
@mysql_select_db($dbname, $mysql_conn) or die("DataBase does not exist");
?>

 

Ray

 

 

Link to comment
Share on other sites

<?php

session_start();

$User = $_SESSION['MM_Username'];

if (!$User) {

header("Location: memberfeatures.php");

}

require_once('Connections/TCO.php');

?>

 

I don't see the problem with this section, as I have this for several of my pages and it successfully connects. :S

 

Is it something to do with the variable $data? I can't see where this is defined? (adapted code from elsewhere you see!).

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.