Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 13th, 2006, 05:09 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting Combobox values from a mySQL database

Hallo All,

I m a bit new to PHP. I am having a mySQL database and I want to get a field value of this database to a combobox. How can I manage to do it? Can anyone help me?

Your attitude determines your altitude
__________________
Your attitude determines your altitude
 
Old November 14th, 2006, 07:53 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You need a few different things. First you need to connect to the database, then you need to query the database and get the array you want to put into the combo box. Then you loop through your array and populate the combo box. Post how much of this you have already done so we know which piece you need answered.


 
Old December 3rd, 2006, 01:06 AM
Registered User
 
Join Date: Dec 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Do you mean something like this (assuming you've got the connect piece down)?

echo "<select>\n";
  $query = "SELECT t.val,t.label FROM t1 AS t ORDER BY t.label ASC";
  $res = @mysql_query($query);
  while($row = mysql_fetch_array($res)) {
    printf("<option value=\"$row[val]\">$row[label]</option>\n");
  }
  mysql-free_result($res);
echo "</select>";

 
Old December 11th, 2006, 05:34 PM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes that was it what I tried to do.

So I am having another problem.

First of all I am having a database named fernschule. It has 3 tables which are:

t_kurse
t_institute
t_ins_kurse

t_kurse includes course names and their IDs like KURSE_NAME KURSE_CODE
t_institute includes institute names which give these courses like INS_NAME INS_CODE
t_ins_kurse includes the foreign keys KURSE_CODE INS_CODE

What I am trying to do is to build a php page which takes the course names into a combobox and their IDs as OPTION value and search the institute(s) which includes the selected course.

So here is my first php page

<?php
/*
* Created on 12.11.2006
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/

$link = mysql_connect('localhost','root','admin');
$db = mysql_select_db("fernschule");
if (!$link) {
die('Could not connect: ' . mysql_error());
}



$sqlquery = "select * from t_kurse ORDER BY KURSE_NAME";
$res = mysql_query($sqlquery);

echo "<FORM ACTION = 'trial1.php' METHOD = 'POST'>";
echo "<SELECT NAME ='t_kurse'>";
while($dsatz = mysql_fetch_assoc($res)) {
$value = $dsatz["KURSE_CODE"];
echo "<OPTION VALUE=".$value.">".$dsatz["KURSE_NAME"]."</OPTION>";
}
echo "<INPUT TYPE = 'SUBMIT' VALUE = 'Suchen'>";
echo "<input type='reset' VALUE = 'Abbrechen'>";

mysql_close($link);
?>

So the trial1.php is

<?php
/*
* Created on 11.12.2006
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/

$link = mysql_connect('localhost','root','admin');
$db = mysql_select_db("fernschule");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$sqlquery = "SELECT t_institute.INS_NAME
FROM t_kurse INNER JOIN (t_institute INNER JOIN t_ins_kurse ON t_institute.INS_CODE=t_ins_kurse.INS_CODE) ON t_kurse.KURSE_CODE=t_ins_kurse.KURSE_CODE
WHERE (((t_kurse.KURSE_CODE)=".$_POST['value']."))";
$res = mysql_query($sqlquery);
while($dsatz = mysql_fetch_assoc($res)) {
echo "<p>".$dsatz["INS_NAME"]."</P>";
}
?>

So it gives error in SQL code. I tried to include the $value variable in the second file as a posted value but it doesn't accept it and gives this error.

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Programme\xampp\htdocs\trial1.php on line 18

So how should I write this SQL query so that it takes the OPTION VALUE $value from trial.php and compares it in trial1.php? Can anyone help me?



Your attitude determines your altitude





Similar Threads
Thread Thread Starter Forum Replies Last Post
vb 5005, checkbox values into combobox Un33k Visual Basic 2005 Basics 0 June 16th, 2007 12:29 AM
ComboBox with DataBase fformula C# 0 March 23rd, 2007 03:41 AM
list of values from postgresql to combobox zamir4eva Beginning PHP 1 January 3rd, 2007 11:29 PM
Store Checkbox values in MySQL Database jkilgore Beginning PHP 1 July 7th, 2005 03:43 PM
Bound ComboBox Database Query dougontour VB.NET 2002/2003 Basics 5 April 20th, 2005 02:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.