2013-05-06 39 views
0

您好我有以下代碼:動態DropDownList的作品,但如何獲得價值? PHP

$dropdown = "<select name='test'>"; 
while($row = mysql_fetch_assoc($result)) { 
    $dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>"; 
} 
$dropdown .= "\r\n</select>"; 
echo $dropdown; 


<form name="input" action="databaseupdate.php" method="post"> 
Select the car you want to edit and fill in the form, followed by clicking the update button. 
<br> 
Carname: <input type="text" name="nameupdate"> 
Year build: <input type="text" name="yearupdate"> 
<input type="submit" value="Update"> 
</form> 

而這一次(databaseupdate.php):

<?php 
$username = "root"; 
$password = "usbw"; 
$hostname = "localhost"; 
$db_name = "examples"; 
$tbl_name = "cars"; 


mysql_connect("$hostname", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

$nameupdate = $_POST['nameupdate']; 
$yearupdate = $_POST['yearupdate']; 
$test = $_POST['test']; 

$query = "UPDATE cars SET name = '$nameupdate', year = '$yearupdate' WHERE id='$test'"; 
mysql_query($query) or die (mysql_error()); 


if($query){ 
echo "Successful"; 
echo "<BR>"; 
echo "<a href='databaseconnect.php'>Back to main page</a>"; 
} 

else { 
echo "ERROR"; 
} 
?> 

因此,該列表由所有的車名從數據庫填充,這是不錯,但是從這一點來說,我不知道如何得到車名所附的id。

我試着用一個查詢來說「wher id ='$ test'」(你可以在我的代碼中看到),但是返回空。我想要什麼

例子:

如果我從下拉列表中選擇「奔馳」,並希望與我想我的代碼知道,奔馳有1

我的ID文本框進行更新不知道我該怎麼做,我希望有人能幫助我。

謝謝。

+0

可否請您在表中包括內部的下拉列表?? – saveATcode 2013-05-06 10:48:56

回答

0

這樣做:(剛加入下拉表單內)所有的

<?php 
    $dropdown = "<select name='test'>"; 
    while($row = mysql_fetch_assoc($result)) { 
     $dropdown .= "\r\n<option value='{$row['id']}'>{$row['name']}</option>"; 
    } 
    $dropdown .= "\r\n</select>"; 
?> 

    <form name="input" action="databaseupdate.php" method="post"> 
    Select the car you want to edit and fill in the form, followed by clicking the update button. 
    <br> 
    <?php echo $dropdown; ?> 
    Carname: <input type="text" name="nameupdate"> 
    Year build: <input type="text" name="yearupdate"> 
    <input type="submit" value="Update"> 
    </form> 
+0

它說成功,但它返回了汽車的名稱,而不是它附帶的標識。而且因爲沒有車號的車名沒有更新。 – 2013-05-06 10:55:08

+0

那是因爲你在選項value.updated中給出了名字,我的代碼在提交後有id。 – 2013-05-06 10:56:38

+0

我已經從數據庫中選擇了*,但它表示ID是未定義的,但是,它看到所有的id行(對於每輛車都有一個未定義的id),任何想法如何解決? – 2013-05-06 11:03:50

0

首先包括在表單內的下拉列表。

其次,如果你想ID爲您的下拉選項中,$result從數據庫爲車名取ID,那麼這樣做:

$dropdown = "<select name='test'>"; 
while($row = mysql_fetch_assoc($result)) { 
$dropdown .= "\r\n<option value='{$row['ID']}'>{$row['name']}</option>"; 
} 
$dropdown .= "\r\n</select>"; 

<form name="input" action="databaseupdate.php" method="post"> 
Select the car you want to edit and fill in the form, followed by clicking the update button. 
<br> 
echo $dropdown; 
Carname: <input type="text" name="nameupdate"> 
Year build: <input type="text" name="yearupdate"> 
<input type="submit" value="Update"> 
</form> 

完成。

0
<form name="input" action="databaseupdate.php" method="post"> 
Select the car you want to edit and fill in the form, followed by clicking the update  button. 
<br> 
Carname: <input type="text" name="nameupdate"> 
Year build: <input type="text" name="yearupdate"> 
<select name='test'> 
<?php while($row = mysql_fetch_assoc($result)) { 
    echo "\r\n<option value='{$row['name']}'>{$row['name']}</option>"; 
}?> 
</select> 
<input type="submit" value="Update"> 
</form> 

databaseupdate.php

$values = $_POST['test']; 

$值包含選擇的值

的主要問題是,你並沒有包含在您選擇到表單標籤。也擺脫POST方法值應該在PHP中使用$ _ POST$ _REQUEST

檢查什麼$ _ POST包含你可以使用函數

var_dump($_POST);