1

i would like to know how can i make a multiple search criteria with 2 or more textboxes and only one submit button. my script is:

$sql = "select * from wp_studenti ";

if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);

$sql .= " WHERE nume= '{$search_term}' ";
}

$query = mysql_query($sql) or die (mysql_error());

echo "<form name ='search_form' method='POST' action='search.php'>"; 
echo "<center><h3>Cauta:</h3> <input type='text' name='search_box' />"; 
echo "<input type='submit' name='search' value='Cauta' /></center>"; 
echo "</form>";

and my results page that shows after search page:

$sql = "select * from wp_studenti ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE nume= '{$search_term}'";
}
echo "<center>\n";
echo "<table border='1'>"; 
echo "<thead>"; 
echo "<tr><th>Id</th>"; 
echo "<th>Nume</th>"; 
echo "<th>Localitate</th>"; 
echo "<th>Judet</th>"; 
echo "<th>Sector Financiar</th>"; 
echo "<th>Link</th></tr>"; 
echo "</thead>";
$rst = mysql_query($sql); 
while($a_row = mysql_fetch_assoc($rst))  { 
echo "<tr>";
echo "<td>"; echo $a_row['id']; echo "</td>";
echo "<td>"; echo $a_row['nume']; echo "</td>";
echo "<td>"; echo $a_row['localitate']; echo "</td>";
echo "<td>"; echo $a_row['judet']; echo "</td>";
echo "<td>"; echo $a_row['sector_financiar']; echo "</td>";
echo "<td>"; echo "<a href='results.php?id={$a_row['id']}'>{$a_row['link']}</a>" ;  echo "</td>";echo "</tr>";
echo "</table>";
1
  • 1
    add another input into your form, name it e.g. search_box2, in after post code add: $search_term2 = mysql_real_escape_string($_POST['search_box2']); then in your sql query add something like this: WHERE nume= '{$search_term}' AND something = '{$search_term2}' Commented Sep 16, 2015 at 6:49

3 Answers 3

1
$sql = "select * from wp_studenti ";

if (isset($_POST['search'])) {
$search_term_by_Cauta = mysql_real_escape_string($_POST['search_box_1']);
$search_term_by_localitate = mysql_real_escape_string($_POST['search_box_2']);

//If you want both search mandatory, use "AND" Operator otherwise use "OR". If you want approximate search use "LIKE" Operator in bellow SQL


$sql .= " WHERE nume= '{$search_term_by_Cauta }' OR localitate = '{$search_term_by_localitate  }' ";
}

$query = mysql_query($sql) or die (mysql_error());

echo "<form name ='search_form' method='POST' action='search.php'>"; 
echo "<center><h3>Cauta:</h3> <input type='text' name='search_box_1' />"; 
echo "<h3>localitate:</h3> <input type='text' name='search_box_2' />"; 
echo "<input type='submit' name='search' value='Cauta' /></center>"; 
echo "</form>";
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome horatiu. But better you will use mysqli, instead of using mysql. Because Mysqli is very secure and Improved driver. MySql is old driver.
1

Well you need another search box:

echo "<center><h3>Cauta:</h3> <input type='text' name='search_box1' /><input type='text' name='search_box2' />"; 

And you need to use that value in your SQL:

if (isset($_POST['search'])) {
    $search_term1 = mysql_real_escape_string($_POST['search_box1']);
    $search_term2 = mysql_real_escape_string($_POST['search_box2']);
    $sql .= " WHERE nume= '{$search_term1}' OR nume= '{$search_term2}'";
}

But you will have to do some thinking about how the search should work, is it supposed to match exactly one OR the other? If you want the text to contain instead of exactly match, you can use the syntax nume LIKE '%searchword%'

Comments

0

Use mysqli instead of mysql, which is depreciated. By PHP, something like this;

<form method='post'>
<input type='hidden' name='srch' val='1'>
Search Type1: <input type='text' name='s1'>
<br>
Search Type2: <input type='text' name='s2'>
<button>Submit</button>
</form>
<?php
if(isset($_POST['srch']))
{
 if(!empty($_POST['s1']))$search = $_POST['s1'];
else if(!empty($_POST['s2']))$search = $_POST['s2'];
else die ('No criteria entered');
rest of your code...

}
?>

Also see functions like mysqli_real_escape for security reasons.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.