Dropdown list from table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asai
Forum Commoner
Posts: 43
Joined: Tue May 04, 2010 6:24 am
Location: Norway

Dropdown list from table

Post by asai »

Hi,
I'm trying to make a form that can select values from a table in mySQL and use this together with other data to post into a new table in my database.
I have made the form to put data into the table, but I can't resolve how to select from table and use it into the form.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Dropdown list from table

Post by social_experiment »

asai wrote:but I can't resolve how to select from table and use it into the form.

Code: Select all

<?php
 // select the relevant data
 $sql = "SELECT field FROM table";
 $qry = mysql_query($sql);

 // start printing your select element
 echo '<select name="dropDownList">';
 // start printing the options
 while ($ary = mysql_fetch_array($qry)) {
     echo '<option value="' . htmlentities($ary['field'], ENT_QUOTES) . '">' . htmlentities($ary['field'], ENT_QUOTES) . '</option>';
 }
 // close select element
 echo '</select>';
?>
This will create a dropdown menu and use whatever values is inside 'field' as options. Substitute field and table for your own information

Hth.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
asai
Forum Commoner
Posts: 43
Joined: Tue May 04, 2010 6:24 am
Location: Norway

Re: Dropdown list from table

Post by asai »

Works!

But now I am trying to put it into my form:

Code: Select all

<html>

<head>

<title>Opprett konto</title>

<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>

<h2>OPPRETT KONTO</h2>

<form method="get" action="legginn_konto.php">



<TABLE WIDTH=30% BORDER=0 CELLPADDING=0 CELLSPACING=0 STYLE="page-break-before: always">

<COL WIDTH=128*>

<COL WIDTH=128*>



<TR VALIGN=TOP>

<TD WIDTH=50%>

<p>Kontonummer: </p></TD>



<TD WIDTH=50%>

<P><input type="text" size="4" maxlength="4" name="kontonummer"></P>

</TD>

</TR>



<TR VALIGN=TOP>

<TD WIDTH=50%>

<p>Kontonavn: </p>



<TD WIDTH=50%>

<p><input type="text" size="30" maxlength="30" name="kontonavn"></p>

</TD>

</TR>



<TR VALIGN=TOP>

<TD WIDTH=50%>

<p>Kontotype: </p>





<TD WIDTH=50%>



<?php

  // select the relevant data



include './config/config.php';

include './config/opendb.php';



  $sql = "SELECT Kontotype FROM Kontotype";

  $qry = mysql_query($sql);

 

 // start printing your select element

  echo '<select name="dropDownList">';

  // start printing the options

  while ($ary = mysql_fetch_array($qry)) {

      echo '<option value="' . htmlentities($ary['Kontotype'], ENT_QUOTES) . '">' . htmlentities($ary['Kontotype'], ENT_QUOTES) . '</option>';

  }

  // close select element

  echo '</select>';



include './config/closedb.php';



 ?>

</TD>

</TR>



</TABLE>





<br>

<input type="submit" value=" Lagre " style="height: 25px; width: 200px" onclick="parent.mainframe.location">

<input type="reset" value=" Avbryt " style="height: 25px; width: 200px" onclick="parent.mainframe.location">



</form>

</FONT>

</body>

</html>
And the "legginn_konto.php" looks like this:

Code: Select all

<html>

<head>

<title>Opprett konto</title>

<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />

</head>



<?php



$a = $_GET['kontonummer'];

$b = $_GET['kontonavn'];

$c = $_GET['Kontotype'];



include './config/config.php';

include './config/opendb.php';



mysql_query("INSERT INTO Kontoplan 

(Kontonummer, Kontonavn, Kontotype) VALUES('$a', '$b', '$c' ) ") 

or die(mysql_error());  



echo "Data skrevet.";



include './config/closedb.php';



?>



</html>
But the value selected from the table doesn't add to table 2. Get an error with duplicate entry.
Am I trying to add to the wrong table?
Any suggestions?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Dropdown list from table

Post by Celauran »

asai wrote:Get an error with duplicate entry.
It would be infinitely more helpful if you posted the error message. Duplicate entry on which field? Is it a unique field?
asai
Forum Commoner
Posts: 43
Joined: Tue May 04, 2010 6:24 am
Location: Norway

Re: Dropdown list from table

Post by asai »

Tried it again, but this time I got no error.
However, the entry selected from table Kontotype did not go into table Kontoplan.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Dropdown list from table

Post by social_experiment »

If you want to add values selected from the dropdown list you have to use $_GET['dropDownList'] to retrieve the selected values
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply