Hi
In my site I have a search function but have hit on a problem that I can only partly solve.
A user makes a search - one or more results are returned. A user can click on an entry and view additional resources on a new page (word2.php) (that's the idea anyway!).
This is the search code;
// Set up our error check and result check array
$error = array();
$results = array();
// First check if a form was submitted.
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
$searchTerms = trim($_GET['search']);
$searchTerms = strip_tags($searchTerms); // remove any html/javascript.
if (strlen($searchTerms) < 1) {
$error[] = "Search terms must be longer than 1 character.";
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}
// If there are no errors, lets get the search going.
if (count($error) < 1) {
$searchSQL = "SELECT * FROM pro_words WHERE";
// grab the search types.
$types = array();
if (count($types) < 1)
$types[] = "`word` LIKE '%{$searchTermDB}%'";
$andOr = isset($_GET['matchall'])?'AND':'OR';
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `word`"; // order by word.
$searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "Your search yielded no results. Sorry.";
}else {
$results = array(); // the result array
$i = 0;
while ($row = mysql_fetch_assoc($searchResult)) {
$results[] = "{$row['word']}<br />";
echo '<h2><tr><td align="left"><a href="word2.php?w=' . $row['word'] . '">' . $row['word'] . '</a></td></tr></h2>';
$i++;
}
}
}
}
function removeEmpty($var) {
return (!empty($var));
}
You'll see that the code links to file word2.php and that's where the problem lies. I want to display additional columns from the table on word2.php.
This is the business part from word2.php;
$query = "SELECT * FROM words";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
echo $row['sentence1'];
}
This returns all entries in the column sentence1.
I think it comes down to how the variables are defined (and where they are defined) but I don't know how to handle that - is there a way of splittig $result for example that would help? Could I use $_POST or $_GET?
I have had partial success in resolving the problem but it's still not working as it should, so that's still a problem.
Thanks in advance for any help - it'd be very much appreciated; please say if anything's unclear.