2013-05-06 37 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);