Go Back   Computer Forums > Web Design | Website Development > Web Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-24-2007, 10:15 PM   #1
In Runtime
 
computerpro's Avatar
 
Join Date: Nov 2004
Posts: 231
Default [PHP] Need Help!

Hey,

I created a PHP search script today and am having a problem with it. It should be fairly easy to solve but whenever I click the "search" button (with the text field empty) it takes me to the searchresults.php page and shows all the indexed info in that table. I hope you understand and can help me!

Someone like root!
__________________

computerpro is offline   Reply With Quote
Old 05-26-2007, 05:09 AM   #2
Fully Optimized
 
Join Date: Jun 2005
Posts: 3,275
Send a message via ICQ to DJ Stephen Send a message via AIM to DJ Stephen Send a message via MSN to DJ Stephen Send a message via Yahoo to DJ Stephen
Default Re: [PHP] Need Help!

I code php too!

Can you please post the code you are having problems with then I can try my best to help you fix it!
__________________

DJ Stephen is offline   Reply With Quote
Old 05-26-2007, 02:01 PM   #3
In Runtime
 
computerpro's Avatar
 
Join Date: Nov 2004
Posts: 231
Default Re: [PHP] Need Help!

Okay here is the form:


<form action="search.php" method="post">
<P ALIGN="Center">
<input type="text" name="search" size="15" maxlength="150" value="" />
<input type="image" name="submit" src="search.jpg" width="75" height="18">
</p>
</form>





search.php


<?php

$search = $_GET['search'];
$search = strip_tags($search);

?>


<?php

mysql_connect("localhost","db_searcher","abc123");


mysql_select_db("db_search");

$search=$_POST["search"];


$result = mysql_query("SELECT * FROM product WHERE keyword LIKE '%$search%'");


while($r=mysql_fetch_array($result))
{


$title=$r["title"];
$description=$r["description"];
$keyword=$r["keyword"];
$url=$r["url"];


echo "$title <br>
$description <br>
$url <br>";

}
?>


It searches keywords
computerpro is offline   Reply With Quote
Old 05-27-2007, 06:53 AM   #4
j03
~~~~~~~~
 
Join Date: Jan 2005
Location: Wales
Posts: 5,556
Send a message via MSN to j03
Default Re: [PHP] Need Help!

If you change the php script to:

PHP Code:
<?php

$search 
$_POST['search'];
$search strip_tags($search);

?>


<?php

mysql_connect
("localhost","db_searcher","abc123");


mysql_select_db("db_search");

$search=$_POST["search"];


$result mysql_query("SELECT * FROM product WHERE keyword LIKE '%$search%'");


while(
$r=mysql_fetch_array($result))
{


$title=$r["title"];
$description=$r["description"];
$keyword=$r["keyword"];
$url=$r["url"];


echo 
"$title <br>
$description <br>
$url <br>";

}
?>
It may work - The form was submitting it as POST data, and the PHP was trying to get GET data.
j03 is offline   Reply With Quote
Old 05-27-2007, 07:06 AM   #5
Fully Optimized
 
Join Date: Jun 2005
Posts: 3,275
Send a message via ICQ to DJ Stephen Send a message via AIM to DJ Stephen Send a message via MSN to DJ Stephen Send a message via Yahoo to DJ Stephen
Default Re: [PHP] Need Help!

PHP Code:
<form action="search.php" method="post">
<
p align="Center">
<
input type="text" name="search" size="15" maxlength="150" value="" />
<
input type="image" name="submit" src="search.jpg" width="75" height="18"
</
p>
</
form
search.php

PHP Code:
<?php
$search 
strip_tags($_POST['search']);
?> 


<?php

mysql_connect
("localhost","db_searcher","abc123"); 
mysql_select_db("db_search"); 

$search strip_tags($_POST["search"]);


$result mysql_query("SELECT * FROM product WHERE keyword LIKE '%$search%'");


while(
$r=mysql_fetch_array($result))



$title $r["title"];
$description $r["description"];
$keyword =$ r["keyword"];
$url $r["url"];


echo 
"$title <br> 
$description <br> 
$url <br>";

}
?>
Have fixed it plus cleaned a few bits of code up.
DJ Stephen is offline   Reply With Quote
Old 05-27-2007, 07:48 PM   #6
In Runtime
 
computerpro's Avatar
 
Join Date: Nov 2004
Posts: 231
Default Re: [PHP] Need Help!

I tried that code and it still doesnt work. When I press submit it still shows me all the sites indexed.

Any other ideas?
computerpro is offline   Reply With Quote
Old 05-29-2007, 12:50 PM   #7
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 7,999
Default Re: [PHP] Need Help!

try putting the query into a query manager,
does it show the same thing/give the same results?

I've updated the script slightly.
the strip_tags function is all well and good, but this is the super beefy guarding against injection attacks version,

PHP Code:
<form action="search.php" method="post">
<p align="Center">
<input type="text" name="search" size="15" maxlength="150" value="" />
<input type="image" name="submit" src="search.jpg" width="75" height="18"> 
</p>
</form>  


 


<?php

mysql_connect
("localhost","db_searcher","abc123"); 
mysql_select_db("db_search"); 


//guard agains injection attack!

    
if( get_magic_quotes_gpc() )
    {
          
$search stripslashes$search );
    }
    
//check if this function exists
    
if( function_exists"mysql_real_escape_string" ) )
    {
          
$search mysql_real_escape_string$search );
    }
    
//for PHP version < 4.3.0 use addslashes
    
else
    {
          
$search addslashes$search );
    }


$result mysql_query("SELECT * FROM product WHERE keyword LIKE '%$search%'");


while(
$r=mysql_fetch_array($result))



$title $r["title"];
$description $r["description"];
$keyword =$ r["keyword"];
$url $r["url"];


echo 
"$title <br> 
$description <br> 
$url <br>";

}
?>
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 05-29-2007, 06:34 PM   #8
In Runtime
 
computerpro's Avatar
 
Join Date: Nov 2004
Posts: 231
Default Re: [PHP] Need Help!

Thanks root, however I got this error:

Parse error: parse error, unexpected T_STRING, expecting T_VARIABLE or '$' in /home/url/public_html/b2/search.php on line 86


Any ideas where it went wrong
computerpro is offline   Reply With Quote
Old 05-30-2007, 10:12 AM   #9
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 7,999
Default Re: [PHP] Need Help!

umm...

even if you didn't split that into two files there are only 54 lines in the code I posted, so I've absolutely no idea!

that's the kind of error you might get if you missed off a semi colon off the end of a line though.
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 05-30-2007, 05:43 PM   #10
In Runtime
 
computerpro's Avatar
 
Join Date: Nov 2004
Posts: 231
Default Re: [PHP] Need Help!

alright, but just to be sure, the code you posted is 100% error free?
__________________

computerpro is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 03:10 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0