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;

}

}

?>

 

Link to comment
Share on other sites

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;

}

}

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.