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.
Dropdown list from table
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Dropdown list from table
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>';
?>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
Re: Dropdown list from table
Works!
But now I am trying to put it into my form:
And the "legginn_konto.php" looks like this:
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?
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>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>Am I trying to add to the wrong table?
Any suggestions?
Re: Dropdown list from table
It would be infinitely more helpful if you posted the error message. Duplicate entry on which field? Is it a unique field?asai wrote:Get an error with duplicate entry.
Re: Dropdown list from table
Tried it again, but this time I got no error.
However, the entry selected from table Kontotype did not go into table Kontoplan.
However, the entry selected from table Kontotype did not go into table Kontoplan.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Dropdown list from table
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