displaying result from a listbox on the same page from mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
nevx
Forum Newbie
Posts: 3
Joined: Fri Dec 17, 2004 2:07 am
Location: indoa

displaying result from a listbox on the same page from mysql

Post by nevx »

ok i know i'm a newbie
here's my database in mysql
table name:jokes
fileds id(primary key),JokeText(text),JokeDate(Date)

i want to display the result dynamically from the listbox when a user selects the jokes,so it diplays all the info about that joke.
my problem is that i don't anything displaying from the database at first only when the user selects something
Here is the code i started working on,,pls help me anyone..
gratefull,,,,thks
nevx


<HTML>
<HEAD>
<TITLE> New Document </TITLE>

</HEAD>

<BODY>
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>

<?php
$username = "";
$password = "";
$hostname = "localhost";

$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";

$selected = mysql_select_db("jokes",$dbh)
or die("Could not select Jokes");

$query = "SELECT JokeText FROM jokes";
$result = mysql_query($query);
print "<SELECT name=joketext>";
while ($line = mysql_fetch_array($result))
{
foreach ($line as $value)
{
print "<OPTION value='$value'";
}
print ">$value</OPTION>";
}
mysql_close($selected);
print "</SELECT><p>";

?>

<BR><br><INPUT TYPE=SUBMIT NAME="submitsel" VALUE="FIND">
</form>
<?

if ("SUBMIT" == $submitsel)
{
$sql = "select JokeText from jokes where JokeText='$value'";
if (mysql_query($sql))
{
echo("<P>Your joke has been added.</P>");
}
else
{
echo("<P>Error adding submitted joke: " .
mysql_error() . "</P>"); } } echo("<P> Here are all the jokes in our database: </P>");





// Request the text of all the jokes
$result = mysql_query("select * from jokes where JokeText='$joketext'");
if (!$result)
{
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) )
{
print "ID:".$row{'id'}." Joke: ".$row{'JokeText'}." "."Date:<b> " . $row{'JokeDate'}."</b><br>";
}

?>


</BODY>
</HTML>
nevx
Forum Newbie
Posts: 3
Joined: Fri Dec 17, 2004 2:07 am
Location: indoa

Post by nevx »

anyone ....out there ...come on hlp pls
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

read the sticky on how to post first...


and then we can ask you how much you have already search yourself.. there are a zillion sites on variables that disappear (register_globals) ..
nevx
Forum Newbie
Posts: 3
Joined: Fri Dec 17, 2004 2:07 am
Location: indoa

????

Post by nevx »

you consider this as helping...
:twisted:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

nobody likes to look at code that is not wrapped in [ php ] tags

Code: Select all

// Request the text of all the jokes
$result = mysql_query("select * from jokes where JokeText='$joketext'");
your comment is out of sync with your code. The first time this page is loaded $joketext won't be set... so your query will be:

Code: Select all

select * from jokes where JokeText=''
Which does not return any rows and therefore show nothing.


my 0.02 eurocents
Post Reply