Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change script to Search

Status
Not open for further replies.

Dukester0122

IS-IT--Management
Mar 18, 2003
587
US
I have a script that returns everything on the table but want to change it to return what the user only enter on the Search field. The current HTML script is:

<html>
<head>
<body>
<Form Action="button.php" Method=Post>
<Input Type="Submit" Name="button2" Value="Customer ID">
</body>
</head>
</html>

The current php script is:
<?
$dsn="customers";
$username="sa";
$password="myaccess2tb";
$sqlconnect=odbc_connect($dsn,$username,$password);
$sqlquery="SELECT custnmbr FROM RM00101;";
$process=odbc_exec($sqlconnect, $sqlquery);
while(odbc_fetch_row($process)){
$custnmbr = odbc_result($process,"custnmbr");
echo "$custnmbr<br>"; }
odbc_close($sqlconnect);
?>
 
First of all, where's the search field? I can only see the submit button. Other than that, you would just need to add WHERE clause to the SELECT sentence with the criteria from the search box. And do some validation and error checking first.
 
I also need some help with the html code if possible and maybe you can tell me how the script would look like.
 
ok I've update my html file:
<html>
<head>
<body>
<Form Action="button.php" Method=Post>
<Input Type="Submit" Name="button2" Value="Seach">
<input type="text" />
</Form>
</body>
</head>
</html>
 
Escuse me Vrag, but I'd like to take a Stab at it if you don't mind.

Dukester,

firts of all you need to give the text box a name: i.e.:
Code:
<input type=text name="searchterm">

Then in the page that processes ht request i.e where your query is located, you have to retrieve the value of the textbox, Since your form is using POST method, you would do something like:
Code:
$term=$_POST['searchterm'];
this assigns the value of the textbox that was submitted with the form to a variable [red]$term[/red].

Then in your query you would do the following:

Code:
...
change this line:
$sqlquery="SELECT custnmbr FROM RM00101;";
...

to
$sqlquery="SELECT custnmbr FROM RM00101[blue] WHERE yourfield LIKE  '" . $term . "'"[/blue];

If the field in your DB is a number you can use [green]=[/green] instead of LIKE if it is a string or text you ahve to use [green]LIKE[/green] and you can use [green]%[/green] for wildcards.

Sya you want to look for anything that begins with whatever the user typed into the text box, you would then add a % before the term. Like this: ... WHERE myfield LIKE '" . $term . "%'";

If you have any questions feel free to post back.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vac,

thanks pitching in. here's what i did on my ht file:
<html>
<head>
<body>
<Form Action="button.php" Method=Post>
<Input Type="Submit" Name="button2" Value="Search">
<Input Type=text name="custnmbr">
</Form>
</body>
</head>
</html>

then here's my php script:
<?php

$dsn="customers";
$username="sa";
$password="myaccess2tb";
$sqlconnect=odbc_connect($dsn,$username,$password);

$term = $_POST['custnmbr'];
$sqlquery="SELECT custnmbr FROM RM00101 WHERE button2 = '".$term."%'";
$process=odbc_exec($sqlconnect, $sqlquery);

while(odbc_fetch_row($process)){
$custnmbr = odbc_result($process,"custnmbr");
echo "$custnmbr<br>";}
odbc_close($sqlconnect);
?>

When ran it's not returning any data. Is there something wrong with it or am I missing anything?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top