Jump to content

match and count user input against Mysql fields


suria

Recommended Posts

Hi there,

 

I am new to PHP & Mysql and I have managed to build a basic search page.

please see the code below

Currently i am able to select single field type and search within....

I would like to search multiple fields at the same time and return the matching

 

E.g.

FieldType  = Sounds

User input = ?

 

FieldType = Generic

User input = ?

 

FieldType = Activity

User input = ?

 

SEARCH BUTTON

 

 

RESULT

 

Echo story ID of matching all input in one row

    and how many in total

Echo story id of matching Two user search

          and how many in total

Echo Story id of matching One

        and how many in total

     

 

Thank you..and please ask me if i didn't make sense..thanks again

 

 

-----search page
$searchtype=$_POST['searchtype']; 
$searchterm=trim($_POST['searchterm']); 

if (!$searchtype || !$searchterm) { 
echo 'You have not entered search details.  Please go back and try again.'; 
exit; 
} 
if (!get_magic_quotes_gpc()){ 
$searchtype = addslashes($searchtype); 
$searchterm = addslashes($searchterm); 
} 
@ $db = new mysqli('localhost', 'root', 'pass', 'dbname'); 
if (mysqli_connect_errno()) { 
echo 'Error: Could not connect to database.  Please try again later.'; 
exit; 
} 
$query = "select * from stories where ".$searchtype."  like '%".$searchterm."%'"; 
$result = $db->query($query); 
$num_results = $result->num_rows; 
echo "<p>Number of records found: ".$num_results."</p>"; 
for ($i=0; $i <$num_results; $i++) { 
$row = $result->fetch_assoc(); 

echo "<strong> <br />Stor ID:</strong> "; 
echo stripslashes($row['id']);

echo.....

} 

$result->free(); 

 

-----

form page

<form action="search.php" method="post"> 

Search by Person:<br /> 
<select name="searchtype"> 
<option value="sounds">Sounds</option> 
<option value="generic">Generic</option> 
<option value="activity">Activity</option> 
</select> 
<br /> 
Enter Search Term:<br /> 
<input name="searchterm" type=”"text" size="40"/> 
<br /> 
<input type="submit" name="submit" value="Search"/> 
</form> 

 

 

-----

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.