Page 1 of 1

Need help building a query form for my php......

Posted: Wed Dec 02, 2009 4:51 am
by pokjak
Hi I am a newbie in php programming and I am just wondering if anybody here can help me on this matter....

I am trying to create a php that has a form where user can search certain thing from a database, meaning when let say I type a certain word in textbox and the program will search the database that is connected to the program.....


This is as far as what I have done


<?
$dbc=odbc_connect("denggi","","");
if (!$dbc)
{exit("Connection Failed: ".$dbc);}
$query="SELECT * FROM msjp_sample";
$rs=odbc_exec($dbc,$query);
if(!$rs)
{exit("Error in SQL");}
echo '<h3>MS Access Powered Database</h3>';
while(odbc_fetch_row($rs))
{
$e_name=odbc_result($rs,"NAMA");
$bulan=odbc_result($rs,"BULAN");
$e_date=odbc_result($rs,"ONSET");

echo '<b>Nama:</b>'.$e_name.'<br>';

echo '<b>Bulan</b>'.$bulan.'<br /> <b>Date:</b>'.$e_date.'<br />
<hr/>';

}
odbc_close($dbc);
echo"</table>"

?>


Based on the code... I have already managed to connect the programming with the database...... I am currently need to know how can I make query with a textbox if I need to search for anything from a certain column within the table......

Re: Need help building a query form for my php......

Posted: Wed Dec 02, 2009 12:31 pm
by AbraCadaver
Build your form with a textbox called "term", then query the database with a WHERE clause for either an exact match or wildcard match. You need to properly sanitize/escape the POST var as I don't know the rules for odbc/access.

Code: Select all

$term = $_POST['term'];
// for a partial match
$query = "SELECT * FROM msjp_sample WHERE NAMA LIKE '*$term*'";
// or for an exact match
$query = "SELECT * FROM msjp_sample WHERE NAMA = '$term'";
You should start by reading up on SQL and MS Access and PHP.

-Shawn

Re: Need help building a query form for my php......

Posted: Wed Dec 02, 2009 6:35 pm
by pokjak
thank you.... I'll try this method......