Note Submitter: liveonaware [at] gmail dhat com
----
<?php
/* Convert your Table fields / columns to variables and use them in your input tags to search the same Table...
Just copy & paste code to see for yourself
*/
//1. connect to database
$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database');
//2. get all table fields in MySQL and store them in an array
$table_fields = array();
$query = "SHOW COLUMNS FROM customers ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$table_fields[] = $row["Field"];
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<?php
//3. make table fields to input tags inside a form
foreach($table_fields as $field_input) { ?>
<tr><td><?php echo $field_input.": "; ?></td><td><input type="text" name="<?php echo $field_input; ?>" /></td></tr>
<?php } ?>
<tr><td></td><td><input type="submit" name="submit" value="Go" style="width: 100px" /></td></tr>
</table>
</form>
<?php
//4. finally, search the same table
if(isset($_POST["submit"])) {
$query2 = "SELECT * FROM customers WHERE id IS NOT NULL ";
//exclude the SUBMIT KEY on the POST Array, add as necessary
//note: MySQL Text Blob, cannot be searched in my kind of Query below because of LIKE % so I added it in excluded list
$excluded_keys_from_post = array('submit', 'sometextarea');
foreach($_POST as $key => $value) {
if(in_array($key, $excluded_keys_from_post)) {
//don't search the POST KEYS in the exclusion array
continue;
}
$query2 .= " AND $key LIKE '" .mysql_real_escape_string($value). "%' ";
}
echo "<hr />";
$result2 = mysql_query($query2);
echo $query2; //just to show what the query looks like
while($row2 = mysql_fetch_array($result2)) {
echo $row2["last_name"]."<br />";
}
$numrows = mysql_num_rows($result2);
if($numrows <= 0) {
echo "No results found.";
}
}
?>
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.