Jump to content

all entries from db passed to output file in error


Bazzaah

Recommended Posts

Hi

 

Hope someone can help me please.

 

I have constructed an audio dictionary and have discovered an error now that I have added a few entries to the database.

 

A user can search the database and can click on a result to be taken to the content associated with the entry he chooses.

 

This is the search function;

 

$search=$_POST["search"];


//get the mysql and store them in $result

$result = mysql_query("SELECT word FROM pro_words WHERE word LIKE '%$search%'");
		        
//get the db content that is specified above

if (mysql_num_rows($result) < 1) {

echo "<br><br><h2> We didn't find anything. Sorry - we did look though.</h2>";

}else {
         
echo '<table align="center" cellspacing="8" cellpadding="8" width="85%"><tr><td align="left"><b>Word</b></td></tr>';

echo "<br><br><h2>Success! Here's what we found:</h2><br>";

while ($r=mysql_fetch_array($result, MYSQLI_ASSOC))

echo '<h2><tr><td align="left"><a href="word.php?w=' . $r['word'] . '">' . $r['word'] . '</a></td></tr></h2>';

}

 

You will see that content is displayed in a new file called word.php.

 

This is relevant code from word.php

 


$query = "SELECT word,word_type1,sentence1,word_type2,sentence2,word_type3,sentence3 FROM pro_words"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){


echo '<div class="colmask rightmenu">';
echo '<div class="colleft">';
echo '<div class="col1">';
echo '<p>Here are some example sentences that show how we use the word; </p>';
echo '<div id="small"><i> ' . $row['word_type1'] . '</i></div>';	
echo '<p><div id="small">' . $row['sentence1'] . '</p></div>';
echo '<div id="small"><i> ' . $row['word_type2'] . '</i></div>';
echo '<p><div id="small">' . $row['sentence2'] . '</p></div>';
echo '<div id="small"><i> ' . $row['word_type3'] . '</i></div>';
echo '<p><div id="small">' . $row['sentence3'] . '</p></div>';

}

 

The problem is that every entry on the database is echoed in word.php whereas I would like only the entries for the word selected to appear.

 

Thanks in advance for any help; do say if you need more info.

 

 

Link to comment
Share on other sites

You can save your search criteria in session. 

$_SESSION['where_caluse'] = " WHERE word LIKE '%".$search."%'";
$result = mysql_query("SELECT word FROM pro_words ".$_SESSION['where_clause']);

 

 

Get session value in word.php file your query looks like

$query = "SELECT word,word_type1,sentence1,word_type2,sentence2,word_type3,sentence3 FROM pro_words".$_SESSION['where_clause']; 

 

Link to comment
Share on other sites

Thanks for that.

 

I should have said that the facility can only be used by registered users - there's already a session variable in existence for the user; where does that leave the session variable you create?

 

I popped that code you kindly provide into the relevant files but doesn't seem to have an effect..I wonder if I'm missing something - is there any significance behind your calling 'where_clause'?

 

Thanks

Link to comment
Share on other sites

Thanks again - it does work but only if there is one search result. If there are two results, it displays two results  on word.php.

 

How do I get it so that, if there are multiple results displayed, then only the chosen entry is displayed on word.php?

 

 

 

Link to comment
Share on other sites

I ran that and no errors, it just returned this;

 

Array

(

    [user_id] => 1

    [first_name] => Myname

    [user_level] => 0

    [where_clause] =>  WHERE word LIKE '%e%'

)

 

which is what I'd expect; no errors though.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.