problem with passing value through a pull down menu in php

Codes here !

Moderators: macek, egami, gesf

szms
New php-forum User
New php-forum User
Posts: 10
Joined: Thu Jun 26, 2003 7:42 am

problem with passing value through a pull down menu in php

Postby szms » Thu Jun 26, 2003 5:33 pm

Hello Every one!!

I am having problem with passing value through a pull down menu and then using that value in a PHP while to execute a query using mysql. So it would be great if you take a look of my code and give me some suggestion. Thank you. :?

Here is my code of HTML pull down menu:

Code: Select all

<?php
require("");
$link = mysql_connect($host,$user,$pass);
if (!$link)
die("Couldn't connect to MySQL");

mysql_select_db($db)
or die("Couldn't open $db: ".mysql_error());


$result = mysql_query("SELECT Name FROM Domain");
$num_rows = mysql_num_rows($result);
if ($num_rows)
{
print "There are currently $num_rows Domain in the Database <p>";
print "<table border = 1>\n";
while($a_row = mysql_fetch_row($result))
{
print "<tr>\n";
foreach ($a_row as $field)
print "\t<td>$field</td>\n";
$Domain_Name[] = $field;
print "</tr>\n";
}
print "</table>\n";
?>


<form action = "ShowDomainDescription1.php" Method = "POST">

Select any of the domain for more information<br>

<select name = "DomainMenu">
<?php
foreach ($Domain_Name as $Existing_Domain)
print " <option value=\"$Existing_Domain \">$Existing_Domain</option>";
?>
</select>
<input type = "submit" value = "Show" >

</form>

<?php }
else
print "There is no data available in the database";
mysql_close($link); ?>


I am getting following worning
Warning: Supplied argument is not a valid MySQL result resource in /home/public_html/ShowDomainDescription1.php on line 12
There is no Domain called , Please check the Domain list again.


Here is my code for ShowDomainDescription1.php

Code: Select all

<?
require("");
$link = mysql_connect($host,$user,$pass);
if (!$link)
die("Couldn't connect to MySQL");

mysql_select_db($db)
or die("Couldn't open $db: ".mysql_error());

if($_POST['DomainMenu'] == $Existing_Domain)
$result = mysql_query("SELECT Description FROM Domain where Name = '$Existing_Domain'");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0)
{
$Domain_Description = mysql_result($result, 0, 'Description');
print "<h1> $Existing_Domain Description </h1>\n";
print "$Domain_Description";
}
else
print "There is no Domain called $Existing_Domain, Please check the Domain list again.";
mysql_close($link);

?>

Flood
New php-forum User
New php-forum User
Posts: 23
Joined: Tue Jun 24, 2003 4:49 am

Postby Flood » Fri Jun 27, 2003 1:26 am

Hi!

In your code

Code: Select all

if($_POST['DomainMenu'] == $Existing_Domain)
$result = mysql_query("SELECT Description FROM Domain where Name = '$Existing_Domain'");
$num_rows = mysql_num_rows($result);


If the condition $_POST['DomainMenu'] == $Existing_Domain is true, then you will store in $result a resource identifier for the results of the SELECT request...
But if it is false, then $result will not be set up and mysql_num_rows($result) will return the error you got :)
As a conclusion, look after the missing {} after your condition.

/Flood


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests