Jump to content

Need help with basic php search function...Aren't I a clown


AcidDriver

Recommended Posts

Hi all!

 

I am realtively new to both php and html, and i was wondering if you could help me out with creating a search function.  I am trying to have a html search interface, and then have it pass varaibles into a php search function, going into a mysql database.  thanks in advance, im sure it will be a stupid mistake.

 

;)

 

HTML Index code

 

<html>

 

<head>

<title>Welcome to the HOPE Database</title>

<link rel="stylesheet" href="text/main.css" type="text/css" />

</head>

 

<body>

<h1>H.O.P.E Volunteer Database Search</h1>

<font class="title">Please enter your database query below.</font>

<form name="search" method="post" action="search.php">

Search by Name: <input type="text" name="find" /> in 

<Select NAME="field">

<Option VALUE="fname">First Name</option><!--Searches according to the volunteers last name -->

<Option VALUE="lname">Last Name</option><!--Searches according to the volunteers first name -->

</Select>

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

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

</form>

<br>

<br>

<a href="volunteer.htm">Input a new volunteer</a>

</body>

</html>

 

PHP Search Code

 

<?php

 

//Displays once search text is run through.

if ($searching =="yes")

{

echo "<h1>Your results: </h1><p>";

 

//If nothing is entered then return no search text.

if ($find == "")

{

echo "<p>Please enter a search item in the query box</";

exit;

}

 

// Else, connect to database

mysql_connect("localhost", "root", "") or die(mysql_error()); //Need to setup dedicated H.O.P.E server to link to mySQL, then input user/server info.

mysql_select_db("hope") or die(mysql_error());

 

 

//Search for search term once in database.

$data = mysql_query("SELECT * FROM hope WHERE upper($field) LIKE'%$find%'");

 

//Display results

while($result = mysql_fetch_array( $data ))

{

echo $result['fname'];

echo " ";

echo $result['lname'];

echo "<br>";

echo $result['idnum'];

echo "<br>";

if ($result['grad'] == 0) //This if statement echos back whether or not the volunteer has graduated. 0 for attending, 1 for alumni, 2 for not a student of UM.

{

echo "This volunteer is an attending student.";

}

if ($result['grad'] == 1)

{

echo "This volunteer is an alumni of the University.";

}

if ($result['grad'] == 2)

{

echo "This volunteer is not a student of the University.";

}

echo "<br>";

echo "Volunteer Hours" .$result['volhrs'];

echo "<br>";

}

//NEED TO GET ALL OF MASTER TEMPLATE INFO TO INPUT SEARCH GUIDELINES

 

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

$matchnum=mysql_num_rows($data);

if ($matchnum == 0)

{

echo "There were no results for you mySQL query.<br><br>";

}

 

//Echo back what the user searched for:

echo "<b>Searched For: </b> " .$find;

echo "<b>Number of Results: </b> " .$matchnum;

}

?>

Link to comment
Share on other sites

of course.

 

Notice: Undefined variable: searching in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 4

 

 

Once we fix this one, however, i know there will be more.  i think it has something to do with passing the html variables into php...

Link to comment
Share on other sites

Of course i checked line 4, not new to programming altogether.  :P

 

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

 

it is in the HTML code, i dont know how to pass it to PHP, unfamiliar to HTML forms, and stuff.

 

I have tried adding the $, and the same error.

 

Thanks for tagging along and your help

Link to comment
Share on other sites

Thanks, you have helped me on that part, that is useful to know.

 

Now on to the hard part.

 

herer is my error message.

 

Notice: Undefined variable: field in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 21

 

Notice: Undefined variable: find in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 21

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 24

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 51

There were no results for you mySQL query.

 

 

Notice: Undefined variable: find in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 58

 

 

 

 

I am going to try and add the $_POST to certain lines to see if it fixes the problem.

Link to comment
Share on other sites

do you have phpmyadmin? I would suggest you test our your query to see if it returns any results

 

so paste this in the sql query

 

SELECT * FROM hope WHERE upper($field) LIKE'%$find%'

 

but replace $find with some value that you know it should be able to find. if you return 0 results, then your query is structured wrong.

Link to comment
Share on other sites

Error

SQL query: 

 

SELECT *

FROM hope

WHERE upper( $field ) LIKE '%$fname%'

LIMIT 0 , 30

 

MySQL said: 

 

#1054 - Unknown column '$field' in 'where clause'

 

 

why are you using the upper function? if you're doing a simple search then you'd do WHERE columnname LIKE '%$fname%' but you HAVE to replace $fname with an actual value that is in your database(if you're doing it in phpmyadmin)

Link to comment
Share on other sites

I am pretty sure that I have inserted the data into the database, and the search brings up this error.

 

SQL query:

 

WHERE fname LIKE '%mike%'

 

MySQL said: 

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE fname LIKE '%mike%'' at line 1

 

fname is the column name, and mike has been inserted in there, I am pretty sure.

Link to comment
Share on other sites

Anyone, I have this project due by today, and I need help, please.

 

Here is the error from my search.

 

Notice: Undefined variable: field in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 21

 

Notice: Undefined variable: find in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 21

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 24

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 51

There were no results for you mySQL query.

 

 

Notice: Undefined variable: find in H:\Documents\My Web Sites\HOPE Search Database\search.php on line 58

Searched For: Number of Results:

 

My code is posted above.  I am really clueless at this point...

 

Edit

Now I cant get this table to insert either....IM dying here.

 

Error

SQL query:

 

CREATE TABLE `hope` (

 

`lname` TEXT NOT NULL ,

`fname` TEXT NOT NULL ,

`idnum` INT NOT NULL ,

`gradyear` INT NOT NULL ,

`grad` ENUM NOT NULL ,

`donor` ENUM NOT NULL ,

`foh` ENUM NOT NULL ,

`org` TEXT NOT NULL ,

`fellow` ENUM NOT NULL ,

`sfellow` ENUM NOT NULL ,

`scholar` ENUM NOT NULL ,

`lreduce` ENUM NOT NULL ,

`address` TEXT NOT NULL ,

`city` TEXT NOT NULL ,

`state` TEXT NOT NULL ,

`zip` TEXT NOT NULL ,

`phone` TEXT NOT NULL ,

`waddress` TEXT NOT NULL ,

`wcity` TEXT NOT NULL ,

`wstate` TEXT NOT NULL ,

`wzip` TEXT NOT NULL ,

`wphone` TEXT NOT NULL ,

`fax` TEXT NOT NULL ,

`cphone` TEXT NOT NULL ,

`email` TEXT NOT NULL ,

`interests` ENUM NOT NULL ,

`project` ENUM NOT NULL ,

`liason` ENUM NOT NULL ,

`pliason` ENUM NOT NULL ,

`comtotalhours` INT NOT NULL ,

`comagency` TEXT NOT NULL ,

`comdate` DATE NOT NULL ,

`comhours` INT NOT NULL ,

`adtotalhours` INT NOT NULL ,

`adagency` TEXT NOT NULL ,

`addate` DATE NOT NULL ,

`adhours` INT NOT NULL ,

`adattorney` TEXT NOT NULL ,

`adattorneycon` TEXT NOT NULL ,

`userlanguage` ENUM NOT NULL ,

UNIQUE (

`lname`

)

) ENGINE = MYISAM

 

MySQL said: 

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL, `donor` ENUM NOT NULL, `foh` ENUM NOT NULL, `org` TEXT NOT NULL, `fell' at line 1

Link to comment
Share on other sites

I am pretty sure that I have inserted the data into the database, and the search brings up this error.

 

SQL query:

 

WHERE fname LIKE '%mike%'

 

MySQL said: 

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE fname LIKE '%mike%'' at line 1

 

fname is the column name, and mike has been inserted in there, I am pretty sure.

 

lol dude, when I said "WHERE fname LIKE '%mike%' " I meant to add that to the end of the "SELECT * FROM table" statement.

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.