Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
MySQL search with PHP??
Old 01-24-2009, 01:50 PM MySQL search with PHP??
Experienced Talker

Posts: 38
Name: carl
Trades: 0
I have a MySQL database setup at my site www.ex-brief.com. I have the PHP scripts to add information to the database, connect, etc. I will paste the code of everything I currently have. Here is where I am having trouble. I am not sure how to write a script that will search the MySQL database. I want it to be basically like the people search on Myspace where you can enter as much or as little information as you like and the searched are returned in order of relevance.

My database is named "exbriefc_profiles", table name is "contacts"

Fields are (so far, will be more possibly) "last" , "first" , "city" , "state"

Here is everything related to this site I have (PHP that is)...need to tie it together to make it work. Any help will be greatly appreciated.

NOTES: My other post for reference: http://www.phpfreaks.com/forums/inde...tml#msg1093910


1) Form To Input Data Into Database (bare bones, not styled)
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
  <title></title>
</head>
<body>
<form action="insert.php" method="post">
First Name: <input name="first" type="text"><br>
Last Name: <input name="last" type="text"><br>
City: <input name="city" type="text"><br>
State: <input name="state" type="text"><br>
<br>
  <input type="submit">
</form>
</body>
</html>
2) PHP Script to Add Information From Form To Database
Code:
<?php

include("dbinfo.inc.php");

$first=$_POST['first'];
$last=$_POST['last'];
$city=$_POST['city'];
$state=$_POST['state'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('','$first','$last','$city','$state')";
mysql_query($query);

if($query) {
echo "Ex Brief Submitted Successfully.";
} else {
echo "The Ex Brief was not processed.  Please review the information you entered for accuracy.";
}

mysql_close();


?>


3) PHP Script to Display Results from Database on webpage
Code:
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result); 

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

?>
<table border="0" cellspacing="2" cellpadding="2">
<tr> 
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">City</font></th>
<th><font face="Arial, Helvetica, sans-serif">State</font></th>
</tr>

<?
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$city=mysql_result($result,$i,"city");
$state=mysql_result($result,$i,"state");
?>

<tr> 
<td><font face="Arial, Helvetica, sans-serif"><? echo "$first $last"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$city"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$state"; ?></font></td>
<?
++$i;
} 
echo "</table>";


?>


4) PHP Script to Search the database, it does not work, it just returns the form page, no results or error message
Code:
<h2>Search</h2>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="first">First Name</option>
<Option VALUE="last">Last Name</option>
<Option VALUE="city">City</option>
<Option VALUE="state">State</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM contacts WHERE upper($field) LIKE'%$find%'");

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['first'];
echo " ";
echo $result['last'];
echo "<br>";
echo $result['city'];
echo "<br>";
echo $result['state'];
echo "<br>";
echo "<br>";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
Exbrief is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-24-2009, 02:07 PM Re: MySQL search with PHP??
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
.....

Last edited by hiptobesquare; 01-24-2009 at 02:09 PM.. Reason: didnt read the question properly, sorry
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 01-24-2009, 02:10 PM Re: MySQL search with PHP??
Experienced Talker

Posts: 38
Name: carl
Trades: 0
Quote:
Originally Posted by hiptobesquare View Post
.....

Whoa... what happened, why did you delete your code?
Exbrief is offline
Reply With Quote
View Public Profile
 
Old 01-24-2009, 02:15 PM Re: MySQL search with PHP??
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
by the looks of things, you arent referring to the posted variables properly in the form handling, should be $_POST[searching] of part 4 above.

when doing this type of thing, i usually build a dynamic query depending on the info gathered from the user, you can utilise more than one search term this way-

Code:
if($_POST[first]!=''){$firstQ="name='$_POST[first]' and ";}
if($_POST[last]!=''){$lastQ="name='$_POST[last]' and ";}

$query=mysql_query("select first, last from TBLE where $firstQ $lastQ id>0");
mark m
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 01-24-2009, 02:21 PM Re: MySQL search with PHP??
Experienced Talker

Posts: 38
Name: carl
Trades: 0
Quote:
Originally Posted by hiptobesquare View Post
by the looks of things, you arent referring to the posted variables properly in the form handling, should be $_POST[searching] of part 4 above.

when doing this type of thing, i usually build a dynamic query depending on the info gathered from the user, you can utilise more than one search term this way-

Code:
if($_POST[first]!=''){$firstQ="name='$_POST[first]' and ";}
if($_POST[last]!=''){$lastQ="name='$_POST[last]' and ";}

$query=mysql_query("select first, last from TBLE where $firstQ $lastQ id>0");
mark m
Should TBLE in the final line be TABLE?

Also, I am pretty much a novice trying to learn. Thsi is a bit above my head in all honesty. Where should I put this code exactly? Sorry for the dumb question, I am still a little turned around after working on this...lol. thanks fo your help.
Exbrief is offline
Reply With Quote
View Public Profile
 
Old 01-24-2009, 02:35 PM Re: MySQL search with PHP??
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
here is part 4 from above, but it should work now. the part i mentioned above would go into part 4 where you retrieve your results half way down.

Code:
<h2>Search</h2>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="first">First Name</option>
<Option VALUE="last">Last Name</option>
<Option VALUE="city">City</option>
<Option VALUE="state">State</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?PHP
//This is only displayed if they have submitted the form
if ($_POST[searching] =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($_POST[find] == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// We preform a bit of filtering
$find = strtoupper($_POST[find]);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified


$data=mysql_query("select * from contacts where ".$_POST[field]." like '%".addslashes($find)."%'");

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['first'];
echo " ";
echo $result['last'];
echo "<br>";
echo $result['city'];
echo "<br>";
echo $result['state'];
echo "<br>";
echo "<br>";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
you will notice that ie aput addslashes() into the database query, this is very important as mysql gets confused with without it. anything that goes into the db via update or insert should also have addslashes() around the values. www.phph.net will give you some explanations about this
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 01-24-2009, 03:22 PM Re: MySQL search with PHP??
Experienced Talker

Posts: 38
Name: carl
Trades: 0
You are my hero. That works perfectly. I have some more questions if you have time. I will ask 1 at a time to make it easier.

1) How exactly do I add security features to my scripts to stop injection attacks, etc? I read the tutorial but I am a little confused. Thanks.
Exbrief is offline
Reply With Quote
View Public Profile
 
Old 01-25-2009, 06:24 PM Re: MySQL search with PHP??
Experienced Talker

Posts: 38
Name: carl
Trades: 0
How exactly do I add security features to my scripts to stop injection attacks, etc? I read the tutorial but I am a little confused. Thanks.
Exbrief is offline
Reply With Quote
View Public Profile
 
Old 01-25-2009, 07:31 PM Re: MySQL search with PHP??
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Did you read the ones in the "sticked" thread ?

http://www.webmaster-talk.com/php-fo...rials-how.html
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-25-2009, 07:33 PM Re: MySQL search with PHP??
Experienced Talker

Posts: 38
Name: carl
Trades: 0
yes, I will read them again, i was super tired and it seemed pretty complicated
Exbrief is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to MySQL search with PHP??
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.20691 seconds with 12 queries