AcidDriver Posted July 15, 2008 Share Posted July 15, 2008 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; } ?> Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 15, 2008 Share Posted July 15, 2008 can you tell us what it's not doing that it should be? or perhaps an error message? Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 15, 2008 Author Share Posted July 15, 2008 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... Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 15, 2008 Share Posted July 15, 2008 Did you think to check line 4 on search.php? if ($searching =="yes") you haven't set $searching to anything Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 15, 2008 Author Share Posted July 15, 2008 Of course i checked line 4, not new to programming altogether. <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 Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 15, 2008 Share Posted July 15, 2008 When you post a form, the page you go to (the action) holds that value in a $_POST array. so in your case, it'd be $_POST['searching'] that you'd be checking to be yes Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 15, 2008 Author Share Posted July 15, 2008 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. Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 15, 2008 Author Share Posted July 15, 2008 It did nothing Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 15, 2008 Share Posted July 15, 2008 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. Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 15, 2008 Author Share Posted July 15, 2008 Error SQL query: SELECT * FROM hope WHERE upper( $field ) LIKE '%$fname%' LIMIT 0 , 30 MySQL said: #1054 - Unknown column '$field' in 'where clause' Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 15, 2008 Share Posted July 15, 2008 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) Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 16, 2008 Author Share Posted July 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 16, 2008 Author Share Posted July 16, 2008 Yes, I am using the Browse tab in phpmyadmin to look at the data, and i have an entry. The search function, WHERE fname LIKE '%Jim%', does not work. It gives me an error message telling me to check the syntax. Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 16, 2008 Author Share Posted July 16, 2008 This project is really important to me, and anyone who helps, I greatly appreciate it. Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 16, 2008 Author Share Posted July 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 16, 2008 Author Share Posted July 16, 2008 can anyone just point me in the general way to solve this problem. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 16, 2008 Share Posted July 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
AcidDriver Posted July 16, 2008 Author Share Posted July 16, 2008 yea i fixed that lol, the search was working, as well as the search that I created, until i took the table down to upload the complete table...which is giving me issues. thanks for helping me here 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.