我正在尝试将已存在于不同MySQL表中的ID写入第二个MySQL表,我在下拉菜单中回显ID和与ID关联的名称。
这是源代码:
<div id="postedit" class="clearfix">
<h2 class="ico_mug">Legg ansatte!</h2>
<form name="form1" method="post" action="<?($_SERVER['DOCUMENT_ROOT']."/ansatte.php")?>">
<div><input name="navn" type="text" size="30" tabindex="1" value="Skriv navn!" /></br></br>
<div id="form_middle_cont" class="clearfix">
<input name="telefon" type="text" size="30" tabindex="1" value="Skriv inn telefon nummer!" /></br></br>
<input name="mobil" type="text" size="30" tabindex="1" value="Skriv inn mobiltelefon nummer!" /></br></br>
<input name="epost" type="text" size="30" tabindex="1" value="Skriv inn epost nummer!" /></br></br>
<input name="bilde" type="text" size="30" tabindex="1" value="Skriv inn link til bilde!" /></br></br>
</div>
<h3>Possisjon: </h3>
<select name="Possisjon" id="possisjon">
<?PHP // Generate a drop-down list of sections.
$result = $connector->query('SELECT position_name FROM position ORDER BY position_id');
// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){
echo '<option value="'.$row['position_id'].'">'.$row['position_name'].'</option>';
}
?>
</select></br></br>
<h3>Stilling:</h3>
<input name="stilling" type="text" size="30" tabindex="1" /></br></br>
<input type="submit" name="Lagre" value="Submit">
</div>
</form>
<?php
// Create an instance of DbConnector
$connector = new DbConnector();
// Setter alt inn i databasen
if($_POST) {
$navn = $_POST['navn'];
$telefon = $_POST['telefon'];
$mobil = $_POST['mobil'];
$epost = $_POST['epost'];
$image = $_POST['bilde'];
$stilling = $_POST['stilling'];
$position = $_POST['possisjon'];
if($navn == '')
echo '<div id="fail" class="info_div"><span class="ico_cancel">Angi en tittel!</span></div><br>';
if($mobil == '')
echo '<div id="fail" class="info_div"><span class="ico_cancel">Angi mer en én tag!</span></div><br>';
if($epost == '')
echo '<div id="fail" class="info_div"><span class="ico_cancel">Velg en seksjon!</span></div><br>';
if($image == '')
echo '<div id="fail" class="info_div"><span class="ico_cancel">Oida! Du har ikke skrevet noe innhold i artikkelen, prøv igjen!</span></div><br><br>';
if($position == '')
echo '<div id="fail" class="info_div"><span class="ico_cancel">lalalala</span></div><br><br>';
if($navn != '' && $telefon != '' && $mobil != '' && $epost != '' && $image != '' && $stilling != '' && $possisjon !='') {
mysql_query("INSERT INTO ansatte (navn, telefon, mobil, epost, image, stilling) VALUES ('{$navn}', '{$telefon}', '{$mobil}', '{$epost}', '{$bilde}', '{$stilling}', '{$possisjon}')");
echo '<div id="success" class="info_div"><span class="ico_success">Suksess!</span></div>';
}
}
?>
任何想法?
干杯, ~PureDarkness~
答案 0 :(得分:2)
更改您的查询
$result = $connector->query('SELECT position_name,position_id
FROM position
ORDER BY position_id');
现在您可以使用以下代码中的position_id