Page 1 of 1

Dropdown list from table

Posted: Sun Nov 20, 2011 5:36 am
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.

Re: Dropdown list from table

Posted: Sun Nov 20, 2011 6:07 am
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.

Re: Dropdown list from table

Posted: Sun Nov 20, 2011 6:55 am
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?

Re: Dropdown list from table

Posted: Sun Nov 20, 2011 9:48 am
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?

Re: Dropdown list from table

Posted: Sun Nov 20, 2011 9:58 am
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.

Re: Dropdown list from table

Posted: Sun Nov 20, 2011 11:43 pm
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