cedtech23
03-22-2007, 07:44 PM
I need help with my logic statement with a simple search engine on a MySQL table.
I have three form text input fields named f_name, l_name and dept. I am using the GET method to gather data the user enters in each input field.
So far I have been able to build a check to see that the submit has been clicked but I am having trouble with assembling the SQL statement because I need for the search engine to be able to filter the results based on the information provided in 1-3 of the fields.
I could use if statements with some and clauses but that would mean that I would have to account for each combination. Something like
If(($_GET[‘f_name’] != “”) || ($_GET[‘l_name’] !=) || ($_GET[‘dept] !=)){
$sql = "SELECT * FROM facform WHERE f_name LIKE '$_GET[f_name]',
l_name LIKE '$_GET[‘l_name]', dept LIKE '$_GET[dept]' ”;
}
The problem with this is that I would have to get every combination and it’s not very scalable. How can I create this SQL statement dynamically?
<?php
if (array_key_exists('submit', $_GET)) {
//SQL statement needed
$db_name = db_test;
$conn = mysql_connect('localhost', 'username', password'') or die(mysql_error());
$db = mysql_select_db($db_name, $conn);
$result = mysql_query($sql, $conn) or die(mysql_error());
}
?>
Can someone help be with my logic and solving this issue with limited steps? I have been using PHP for 2 weeks so please can you keep it simple
Thanks
I have three form text input fields named f_name, l_name and dept. I am using the GET method to gather data the user enters in each input field.
So far I have been able to build a check to see that the submit has been clicked but I am having trouble with assembling the SQL statement because I need for the search engine to be able to filter the results based on the information provided in 1-3 of the fields.
I could use if statements with some and clauses but that would mean that I would have to account for each combination. Something like
If(($_GET[‘f_name’] != “”) || ($_GET[‘l_name’] !=) || ($_GET[‘dept] !=)){
$sql = "SELECT * FROM facform WHERE f_name LIKE '$_GET[f_name]',
l_name LIKE '$_GET[‘l_name]', dept LIKE '$_GET[dept]' ”;
}
The problem with this is that I would have to get every combination and it’s not very scalable. How can I create this SQL statement dynamically?
<?php
if (array_key_exists('submit', $_GET)) {
//SQL statement needed
$db_name = db_test;
$conn = mysql_connect('localhost', 'username', password'') or die(mysql_error());
$db = mysql_select_db($db_name, $conn);
$result = mysql_query($sql, $conn) or die(mysql_error());
}
?>
Can someone help be with my logic and solving this issue with limited steps? I have been using PHP for 2 weeks so please can you keep it simple
Thanks