Jump to content

[SOLVED] MAJOR UNKNOWN PROBLEM (I'm sure it's obvious... I'm only a beginner...)


kaimason1

Recommended Posts

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;

}

}

?>

 

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;

}

}

?>

 

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.

 

 

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 ''

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.

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;
}
}
}
?>

O.K., did that, now I have no result at all. It isn't even echoing any search terms!!!!! :o :o :o :o :(>:(:P:-[

( 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;
}
}
}
?>

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.

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;
}
}
}
?>

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?

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.