Page 1 of 1

[b]Search code and change data code please.[/b]

Posted: Wed Feb 05, 2003 3:05 am
by celtic
Hi there, I seem to be doing everything the hard way. 8O I used some code for accessing a database I created but now I want to be able just to search the database for a part of a "word" and pull up all the results. I also want to be able to do changes to the data i.e. "the number of "parts" change so I want to change the result in the field which would then update the database, I need some code for this please, hope this makes sense ... here is what I have so far:

<html>
<head>
<title>Retieving Data from a Database</title>
<body>
<div align="center"><strong><font size="5">Parts Database</font></strong></div></p>

<?php
// Set the variables for the database access:
$hostname="localhost";
$username="xxxxx";
$password="xxxxx";
$dbname="xxxxxx";
$usertable="xxxxxx";

$Link = Mysql_connect ($hostname, $username, $password);

$Query = "SELECT * from $usertable";
$Result = mysql_db_query ($dbname, $Query, $Link);

//Create a table.
print("<TABLE BORDER=1 WIDTH=\"75%\" CELLSPACING=1 CELLPADDING=1 ALIGN=CENTER>\n");
print("<TR ALIGN=CENTER VALIGN=TOP>\n");
print("<TD ALIGN=CENTER VALIGN=TOP><strong><font size=\3\>Parts<TD>\n");
print("<TD ALIGN=CENTER VALIGN=TOP><strong><font size=\3\>Mfr<TD>\n");
print("<TD ALIGN=CENTER VALIGN=TOP><strong><font size=\3\>Spec<TD>\n");
print("<TD ALIGN=CENTER VALIGN=TOP><strong><font size=\3\>Quantity<TD>\n");
print("<TD ALIGN=CENTER VALIGN=TOP><strong><font size=\3\>Cost<TD>\n");
print("</TR>\n");

//Fetch the results from the database.
while($Row=mysql_fetch_array($Result)){
print("<TR ALIGN=CENTER VALIGN=TOP>\n");
print("<TD ALIGN=CENTER VALIGN=TOP>$Row[parts]<TD>\n");
print("<TD ALIGN=CENTER VALIGN=TOP>$Row[Mfr]<TD>\n");
print("<TD ALIGN=CENTER VALIGN=TOP>$Row[Spec]<TD>\n");
print("<TD ALIGN=CENTER VALIGN=TOP>$Row[Quantity]<TD>\n");
print("<TD ALIGN=CENTER VALIGN=TOP>$Row[Cost]<TD>\n");
print("</TR>\n");
}
mysql_close ($Link);
print("</TABLE>\n");</p>

$sql = "select count(*) from ied_data" ;
$result = mysql_query($sql) ;
$row = mysql_fetch_array($result) ;
echo " Database has " . $row[0] . "rows." ;
?>

</body>
</html>

Posted: Tue Feb 11, 2003 1:21 pm
by hurkle
hmm.. I'm not sure what you mean by searching on part of a word, but you could try something like this.

Lets say you have a form like this..

Code: Select all

<FORM name = 'frm_search' method = 'post' action = 'search.php'>

<INPUT type = 'text' name = 'txt_search'>

<INPUT type = 'submit' value = 'submit'>
you could use a script like this..

Code: Select all

<?php

if (isset($_POST&#1111;'txt_search']))

&#123;

$str_search = $_POST&#1111;'txt_search'];

$query= "SELECT * FROM tbl_parts WHERE ";
$query .= " parts LIKE '%" . $str_search . "%' OR ";
$query .= " Mfr LIKE '%" . $str_search . "%' OR ";
$query .= " Spec LIKE '%" . $str_search . "%'  ";

...
display your results
...

&#125;
else
&#123;
echo "No search criteria from search form<BR>"
&#125;

?>
If it works, this code should return rows where fields named parts, Mfr, or Spec contain data that has your search criteria in it.

Hope this helps get you started.