kaimason1 Posted January 17, 2009 Share Posted January 17, 2009 I'm trying to create my very first search script from scratch. I haven't noticed any problems with the following script, but I get nothing on searches I know should come out with results. HELP??? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>My Search</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <FORM METHOD="GET" action="search.php"> <P>Please enter your search words here and specify the type of data you input: <INPUT type="text" name="search" size=30 maxlength=70> <SELECT name="type" size="1"> <OPTION value="Name">Name</OPTION> <OPTION value="Cell">Cell Phone Number</OPTION> <OPTION value="Email">Email</OPTION> </SELECT> </P> <INPUT type="submit" value="Submit"> </FORM> </BODY> </HTML> <? $search=@$_GET['search']; $type=@$_GET['type']; if($search=""){ die("Please enter a search."); }else if($type="Name"){ $query="SELECT * FROM TEST1_contacts WHERE First_Name LIKE '$search' OR Last_Name LIKE '$search'"; }else if($type="Cell"){ $query="SELECT * FROM TEST1_contacts WHERE Phone_Number LIKE '$search'"; }else if($type="Email"){ $query="SELECT * FROM TEST1_contacts WHERE Email LIKE '$search'"; }else{ die("Congratulations -- you achieved the impossible rating of hacker. How else did I receive an inaccurate data type, hmm?"); } $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); @mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); $result=mysql_query($query); $num=mysql_numrows($result); if($num==0){ die("There seems to be no records matching your search. Interesting..."); }else{ $i=0; while($i<0){ $first=mysql_result($result,$i,"First_Name"); $last=mysql_result($result,$i,"Last_Name"); $cell=mysql_result($result,$i,"Phone_Number"); $email=mysql_result($result,$i,"Email"); echo("<b>$first $last</b><br>Cell: $cell<br>Email: $email<br><hr><br>"); ++$i; } } ?> Quote Link to comment Share on other sites More sharing options...
dclamp Posted January 17, 2009 Share Posted January 17, 2009 please edit your post using PHP tags. It makes it easy to read your code. Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 17, 2009 Share Posted January 17, 2009 when it fails do you get "There seems to be no records matching your search. Interesting..." or does it fail to display the results? Scott. Quote Link to comment Share on other sites More sharing options...
Prismatic Posted January 17, 2009 Share Posted January 17, 2009 $num=mysql_numrows($result); change to $num=mysql_num_rows($result); Quote Link to comment Share on other sites More sharing options...
shlumph Posted January 17, 2009 Share Posted January 17, 2009 I wouldn't suggest suppressing your mysql_select_db call to the database, lol. I'm sure the rest of your code won't like it if that error is suppressed. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 17, 2009 Author Share Posted January 17, 2009 when it fails do you get "There seems to be no records matching your search. Interesting..." or does it fail to display the results? Scott. I get "There seems to be no records matching your search. Interesting...". Here is my updated script, still getting the same problem. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>My Search</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <FORM METHOD="GET" action="search.php"> <P>Please enter your search words here and specify the type of data you input: <INPUT type="text" name="search" size=30 maxlength=70> <SELECT name="type" size="1"> <OPTION value="Name">Name</OPTION> <OPTION value="Cell">Cell Phone Number</OPTION> <OPTION value="Email">Email</OPTION> </SELECT> </P> <INPUT type="submit" value="Submit"> </FORM> </BODY> </HTML> <? //Retrieve variables from HTML form// $search=@$_GET['search']; $type=@$_GET['type']; //Create MySQL query// if($search=""){ die("Please enter a search."); }else if($type="Name"){ $query="SELECT * FROM TEST1_contacts WHERE First_Name LIKE '$search' OR Last_Name LIKE '$search'"; }else if($type="Cell"){ $query="SELECT * FROM TEST1_contacts WHERE Phone_Number LIKE '$search'"; }else if($type="Email"){ $query="SELECT * FROM TEST1_contacts WHERE Email LIKE '$search'"; }else{ die("Congratulations -- you achieved the impossible rating of hacker. How else did I receive an inaccurate data type, hmm?"); } //Connect to database CENSORED INFORMATION// $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); @mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); //Search Results// $result=mysql_query($query) or die (mysql_error()); $num=mysql_num_rows($result); if($num==0){ die("There seems to be no records matching your search. Interesting..."); }else{ $i=0; while($i<0){ $first=mysql_result($result,$i,"First_Name"); $last=mysql_result($result,$i,"Last_Name"); $cell=mysql_result($result,$i,"Phone_Number"); $email=mysql_result($result,$i,"Email"); echo("<b>$first $last</b><br>Cell: $cell<br>Email: $email<br><hr><br>"); ++$i; } } ?> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 17, 2009 Share Posted January 17, 2009 use the [ code ]//code goes here [/ code] (without the spaces) tags try adding echo $query; to check and make sure your query is what you expect try pasting it into php my admin or something and see what results you get Scott. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 17, 2009 Author Share Posted January 17, 2009 This is what the php is interpreting $query as for a name $type : ('' means two 's, not one ".) SELECT * FROM TEST1_contacts WHERE First_Name LIKE '' OR Last_Name LIKE '' Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 17, 2009 Share Posted January 17, 2009 try changing these two lines and why do you have a @ on them? $search=@$_GET['search']; $type=@$_GET['type']; to and $search=trim(mysql_real_escape_string($_GET['search'])); $type=trim(mysql_real_escape_string($_GET['type'])); change your submit button to <INPUT type="submit" name="submit" value="Submit"> then put all your php in if(isset($_GET['submit']) && $_GET['submit'] == "Submit"){ //rest of the code here } Scott. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 17, 2009 Author Share Posted January 17, 2009 It still interprets my query as SELECT * FROM TEST1_contacts WHERE First_Name LIKE '' OR Last_Name LIKE '' and outputs "There seems to be no records matching your search. Interesting..." ??? ??? ??? Anyway, here again is my script: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>My Search</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <FORM METHOD="GET" action="search.php"> <P>Please enter your search words here and specify the type of data you input: <INPUT type="text" name="search" size=30 maxlength=70> <SELECT name="type" size="1"> <OPTION value="Name">Name</OPTION> <OPTION value="Cell">Cell Phone Number</OPTION> <OPTION value="Email">Email</OPTION> </SELECT> </P> <INPUT type="submit" name="submit" value="Submit"> </FORM> </BODY> </HTML> <? //Connect to database// $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); @mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); if(isset($_GET['submit']) && $_GET['submit'] == "Submit"){ //Retrieve variables from HTML form $search=trim(mysql_real_escape_string($_GET['search'])); $type=trim(mysql_real_escape_string($_GET['type'])); //Create MySQL query// if($search=""){ die("Please enter a search."); }else if($type="Name"){ $query="SELECT * FROM TEST1_contacts WHERE First_Name LIKE '$search' OR Last_Name LIKE '$search'"; }else if($type="Cell"){ $query="SELECT * FROM TEST1_contacts WHERE Phone_Number LIKE '$search'"; }else if($type="Email"){ $query="SELECT * FROM TEST1_contacts WHERE Email LIKE '$search'"; }else{ die("Congratulations -- you achieved the impossible rating of hacker. How else did I receive an inaccurate data type, hmm?"); } echo $query; //Search Results// $result=mysql_query($query) or die (mysql_error()); $num=mysql_num_rows($result); if($num==0){ die("There seems to be no records matching your search. Interesting..."); }else{ $i=0; while($i<0){ $first=mysql_result($result,$i,"First_Name"); $last=mysql_result($result,$i,"Last_Name"); $cell=mysql_result($result,$i,"Phone_Number"); $email=mysql_result($result,$i,"Email"); echo("<b>$first $last</b><br>Cell: $cell<br>Email: $email<br><hr><br>"); ++$i; } } } ?> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 17, 2009 Share Posted January 17, 2009 if you look at the URL is there a part like "search=whateveryoutyped"? Scott. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 ...?search=whatitype&type=Name&submit=Submit is what i get when i do a search in the URL. Still getting nothing. Quote Link to comment Share on other sites More sharing options...
trq Posted January 22, 2009 Share Posted January 22, 2009 In your else if's your using the assignment operator = and you ought be using the comparison operator ==. eg; Change.... }else if($type="Name"){ to.... } else if($type == "Name") { Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 O.K., did that, now I have no result at all. It isn't even echoing any search terms!!!!! :o :o ( on line 46 I inserted "echo $search;" as an error test... and it isn't outputting anything!) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>My Search</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <FORM METHOD="get" action="search.php"> <P>Please enter your search words here and specify the type of data you input: <INPUT type="text" name="search" size=30 maxlength=70> <SELECT name="type" size="1"> <OPTION value="Name">Name</OPTION> <OPTION value="Cell">Cell Phone Number</OPTION> <OPTION value="Email">Email</OPTION> </SELECT> </P> <INPUT type="submit" name="submit" value="Submit"> </FORM> </BODY> </HTML> <? if(isset($_GET['submit']) && $_GET['submit'] == "Submit") { //Connect to database (censored)// $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); @mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); //Retrieve variables from HTML form $search=trim(mysql_real_escape_string($_GET['search'])); $type=trim(mysql_real_escape_string($_GET['type'])); //Create MySQL query// if($search == "") { die("Please enter a search."); } else if($type == "Name") { $query="SELECT * FROM TEST1_contacts WHERE First_Name LIKE '$search' OR Last_Name LIKE '$search'"; } else if($type == "Cell") { $query="SELECT * FROM TEST1_contacts WHERE Phone_Number LIKE '$search'"; } else if($type == "Email") { $query="SELECT * FROM TEST1_contacts WHERE Email LIKE '$search'"; } else{ die("Congratulations -- you achieved the impossible rating of hacker. How else did I receive an inaccurate data type, hmm?"); } echo $search; //Search Results// $result=mysql_query($query) or die (mysql_error()); $num=mysql_num_rows($result); if($num == 0) { die("There seems to be no records matching your search. Interesting..."); } else{ $i=0; while($i<$num){ $first=mysql_result($result,$i,"First_Name"); $last=mysql_result($result,$i,"Last_Name"); $cell=mysql_result($result,$i,"Phone_Number"); $email=mysql_result($result,$i,"Email"); echo("<b>$first $last</b><br>Cell: $cell<br>Email: $email<br><hr><br>"); ++$i; } } } ?> Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 Get rid of those @ characters in your code. They suppress errors. Suppressing errors means you can't see what and where the errors are. Particularly this: @mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); You are telling it to suppress the error, then to give an error if it doesn't work. But you won't see the error unless you remove the @ mark. Using @ is sloppy programming, except for very few cases where its necessary. They are the exception though, and don't pertain to this code. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 thank you, I did that now... im still left with absolutely nothing though... :'( :'( :'( Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 Post your code again as it is now. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 here you go. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>My Search</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <FORM METHOD="get" action="search.php"> <P>Please enter your search words here and specify the type of data you input: <INPUT type="text" name="search" size=30 maxlength=70> <SELECT name="type" size="1"> <OPTION value="Name">Name</OPTION> <OPTION value="Cell">Cell Phone Number</OPTION> <OPTION value="Email">Email</OPTION> </SELECT> </P> <INPUT type="submit" name="submit" value="Submit"> </FORM> </BODY> </HTML> <? if(isset($_GET['submit']) && $_GET['submit'] == "Submit") { //Connect to database// $username="mekam2_kai"; $password="753159"; $database="mekam2_kai1"; mysql_connect(localhost,$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); //Retrieve variables from HTML form $search=trim(mysql_real_escape_string($_GET['search'])); $type=trim(mysql_real_escape_string($_GET['type'])); //Create MySQL query// if($search == "") { die("Please enter a search."); } else if($type == "Name") { $query="SELECT * FROM TEST1_contacts WHERE First_Name LIKE '$search' OR Last_Name LIKE '$search'"; } else if($type == "Cell") { $query="SELECT * FROM TEST1_contacts WHERE Phone_Number LIKE '$search'"; } else if($type == "Email") { $query="SELECT * FROM TEST1_contacts WHERE Email LIKE '$search'"; } else{ die("Congratulations -- you achieved the impossible rating of hacker. How else did I receive an inaccurate data type, hmm?"); } echo $search; //Search Results// $result=mysql_query($query) or die (mysql_error()); $num=mysql_num_rows($result); if($num == 0) { die("There seems to be no records matching your search. Interesting..."); } else{ $i=0; while($i<$num){ $first=mysql_result($result,$i,"First_Name"); $last=mysql_result($result,$i,"Last_Name"); $cell=mysql_result($result,$i,"Phone_Number"); $email=mysql_result($result,$i,"Email"); echo("<b>$first $last</b><br>Cell: $cell<br>Email: $email<br><hr><br>"); ++$i; } } } ?> Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 First: You should use <?php instead of <?. Some servers won't accept the short tag, and occasionally servers will change their settings to not accept the short tag, and all of a sudden your scripts stop working for some unexplained reasoning. Second: Is this: echo $search; not echoing anything? Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 echo $search; is not echoing a thing. Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 Directly after this code: if(isset($_GET['submit']) && $_GET['submit'] == "Submit") { add this: die("entered if statement"); and run the code. Your script should die with that text on the screen. Let us know what happens. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 that must be the source of my problem. I still get absolutely nothing. Any suggestions on how to fix that? Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 Your 'if' loop isn't being entered. So there is a problem with this code: if(isset($_GET['submit']) && $_GET['submit'] == "Submit") { You must not be setting submit=Submit in the URL. Are you sure you don't want that to read $_POST['submit']? Your submit button will have to have a value of submit for this to work. Or maybe you forgot to capitalize Submit in the URL. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 Well... truth be told, i don't know. I included my HTML in the same document, and i have been entering my searches using that. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 23, 2009 Author Share Posted January 23, 2009 I don't know what happened, but now it's telling me it entered the if statement AND THE SEARCH WORKED!!!!!!!! THANK YOU EVERYONE!!!!!!!!! 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.