7 Replies - 3025 Views - Last Post: 04 March 2011 - 05:23 AM Rate Topic: -----

#1 nareshmp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 11

Combo Box Problem

Posted 03 March 2011 - 05:25 AM

Hi,

I am doing a small project in php.How i have to fetch data from database into combo box and i have to retrieve the selected option from the database.

kindly help me to sort out the problem.


Regards,
Naresh Kumar M P

Attached File(s)


This post has been edited by Dormilich: 03 March 2011 - 05:40 AM
Reason for edit:: changed title

Is This A Good Question/Topic? 0
  • +

Replies To: Combo Box Problem

#2 nareshmp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 11

Re: Combo Box Problem

Posted 03 March 2011 - 05:31 AM

hi,
I have some error in this coding,I want to fetch data from database and display accordingly to the selection in the combo box.

       <?php
	
	mysql_connect ("localhost", "root","")  or die (mysql_error());
	mysql_select_db ("Model");
	 
	$stage2 = $_POST['stage2'];
	 
	
	$sql = mysql_query("select * from product where brmodel like '%$stage2%'");
	
	$row = mysql_fetch_array($sql)
		 if ($row ='brmodel')
		 {
	  
	    echo '<br/> specific: '.$row['specific'];
	   
	    }
		?>




Kindly give some idea to sort out this problem.


Naresh

This post has been edited by Dormilich: 03 March 2011 - 05:40 AM
Reason for edit:: please use [code] /* your code source here */ [/code] tags when posting code

Was This Post Helpful? 0
  • +
  • -

#3 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Combo Box Problem

Posted 03 March 2011 - 06:33 AM

Few things on line 12:

1. $row is the fetched row in form of an associative array. You can't use the data of the row directly with it. Use it like $row[0], $row[1] and so on...

2. You are using assignment operator instead of comparison operator.

This post has been edited by chinchang: 03 March 2011 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10819
  • View blog
  • Posts: 40,334
  • Joined: 27-December 08

Re: Combo Box Problem

Posted 03 March 2011 - 09:25 PM

In the future, please remember to post your specific problems or errors.

Here, you are missing a semi-colon at the end of this statement.
$row = mysql_fetch_array($sql)  



Also, always separate your PHP and HTML. Not good practice to mix the two, as it makes it harder to maintain your design and application.
Was This Post Helpful? 0
  • +
  • -

#5 nareshmp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 11

Re: Combo Box Problem

Posted 03 March 2011 - 11:29 PM

Hi,
I have problem in retrieving data from database into combo box.By selecting particular name,it should display the designation of that person.

My table name stud
Feilds are: empno name desig

My coding :

<?php
if(isset($_GET['empno']))
{
$username="root";
$password="naresh";
$database="products";
$name1=$_GET['name'];
$chandle = mysql_pconnect("localhost", $username, $password)
  or die("Connection Failure to Database");
mysql_select_db($database, $chandle) or die ("Database not found.");

$query1="select * from stud where name='$name1' ";
$result=mysql_query($query1);
$x=0;
$num=mysql_numrows($result);

while($x<$num)
  {
     $f1 = mysql_result($result,$x,1) or die("Failed Query");
    echo "".$f1;
    $x++;
  }
}
else
{?>

Select the roll <form action='<?php echo $_SERVER['PHP_SELF'];?>'>
<select name='name' size="1">
<option value=1>Ram</option>
<option value=2>Arun</option>
<option value=3>Karthik</option>
<option value=4>Naresh</option>
<option value=5>Bala</option>
<option value=6>Babu</option>

</select>
<input type="submit">
</form>
<?php }?>





Kindly give some tips to work out this problem.


Regards,
Naresh Kumar M P
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Combo Box Problem

Posted 03 March 2011 - 11:48 PM

Well it's not going to pull through a name if that's what you're expecting it to do, you're setting the value attribute on your options, so that is what is going to be in $_GET['name'], not the text you desire.

BTW, this isn't a real function:
$num=mysql_numrows($result);


It's mysql_num_rows($result); that you need.

Also, may I suggest that you, rather than using mysql_result and a numeric loop, do something like this:
while($row = mysql_fetch_array($result) {
    echo $row[1];
}


.. and then may I suggest that rather than using index-based arrays to access your rows data, you change to using column names. It's wayyyy easier to know what you're doing then, ie:
while($row = mysql_fetch_assoc($result) {
    echo $row['name'];
}

This post has been edited by RudiVisser: 03 March 2011 - 11:49 PM

Was This Post Helpful? 1
  • +
  • -

#7 nareshmp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 11

Re: Combo Box Problem

Posted 04 March 2011 - 12:21 AM

View PostRudiVisser, on 03 March 2011 - 11:48 PM, said:

Well it's not going to pull through a name if that's what you're expecting it to do, you're setting the value attribute on your options, so that is what is going to be in $_GET['name'], not the text you desire.

BTW, this isn't a real function:
$num=mysql_numrows($result);


It's mysql_num_rows($result); that you need.

Also, may I suggest that you, rather than using mysql_result and a numeric loop, do something like this:
while($row = mysql_fetch_array($result) {
    echo $row[1];
}


.. and then may I suggest that rather than using index-based arrays to access your rows data, you change to using column names. It's wayyyy easier to know what you're doing then, ie:
while($row = mysql_fetch_assoc($result) {
    echo $row['name'];
}



Hi,


Thank you, its working....

Regards,
Naresh
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Combo Box Problem

Posted 04 March 2011 - 05:23 AM

Merged duplicate topics. Do NOT create a new topic when you already have one on the same subject!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1