简体   繁体   English

如何在PHP的编辑表单中填充下拉框

[英]how to populate a dropdown box in a edit form in php

UPDATE TO Below: 更新到下面:

I changed the code a bit and am having a bit of success but I am still off on one thing. 我稍微修改了代码,并取得了一些成功,但我仍然一无所获。 I changed the line 我换了线

<option value="<?php echo $row[brokername]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>

to

<option value="<?php echo $brokerlistrow[name]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>

Now it shows the correct select name and actually saves a changed value in the database however, instead of saving the name id number, it saves the actual name. 现在,它显示正确的选择名称,并实际上将更改后的值保存在数据库中,但是,它保存实际名称,而不是保存名称ID号。 a print_r of $row gives me: $ row的print_r给我:

Array ( [0] => 4 [id] => 4 [1] => 6 [brokername] => 6 [2] => Kims Boat [boatname] => Kims Boat ) 数组([0] => 4 [id] => 4 [1] => 6 [经纪人名称] => 6 [2] => Kims Boat [boatname] => Kims Boat)

The correct brokerlist.id for that selected user is 6 but instead of saving 6 to the boatlist.name it save the brokerlist.name field instead of the brokerlist.id. 所选用户的正确brokerlist.id是6,但不是将6保存到boatlist.name,而是保存brokerlist.name字段而不是brokerlist.id。

What can I change to have it save the brokerlist.id as opposed to the physical name? 我可以更改使其保存brokerlist.id而不是物理名称吗? :) :)

Thank you in advance! 先感谢您!

(here is the new edit page code just in case: (以下是新的编辑页面代码,以防万一:

<?php

    include_once('db.php');     

    if(isset($_GET['edit']))

    {

    $id = $_GET['edit']; 

    $res= mysql_query("SELECT * FROM boatlist WHERE id='$id'"); 

    $row= mysql_fetch_array($res);   

    $brokerlistquery = mysql_query("SELECT * FROM brokerlist");

    print_r($row);

    }


    if( isset($_POST['newbrokername']) && isset($_POST['newboatname']))    
       {    
         $newbrokername = $_POST['newbrokername'];
         $newboatname = $_POST['newboatname'];     
         $id= $_POST['id'];

         $sql = "UPDATE boatlist SET brokername='$newbrokername', boatname='$newboatname' WHERE id='$id'";

         $res= mysql_query($sql) or die("Could not update".mysql_error());

         echo "<meta http-equiv='refresh' content='0;url=testaddboat.php'>";

    }



?>

<table class="centerwithroom">    
<form action="testeditboat.php" method="POST">    
<tr>    
<td>New Broker Name </td><td>
<select name='newbrokername'>
  <?php
    while ($brokerlistrow = mysql_fetch_array($brokerlistquery)) {
  ?>
<option value="<?php echo $brokerlistrow[name]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
</td>

<tr>
<td>Boat Name:</td><td><input type="text" name="newboatname" value="<?php echo $row[boatname]; ?>"/></td>
</tr>    

</tr>

<td></td><td><input type="submit" value=" Update "/></td>

</tr>

<input type="hidden" name="id" value="<?php echo $row[id]; ?>"/>    

</form>
</table>

(ORIG POST Starts Here) (原帖从这里开始)

This is my first post and I will try to be as thorough as possible. 这是我的第一篇文章,我将力求做到透彻。 This is just a rudimentary form and I have not added any security in it at all as I am just trying to make it work, then get to the rest :) Being new to programming my code may look bad to some of you experts and I would love any examples you could give. 这只是一个基本形式,我只是想使它工作,然后再介绍其他内容,所以我没有在其中添加任何安全性:)对我的代码进行编程可能对某些专家和我来说都是不好的会喜欢你能举的任何例子。

On to the project. 进入项目。 I have two mysql tables, one called brokerlist with two fields id and name. 我有两个mysql表,其中一个称为brokerlist,具有两个字段id和name。 the second table is called boatlist with three fields id, brokername, and boatname. 第二个表称为Boatlist,具有三个字段ID,BrokerName和BoatName。 The brokername field contains the value of the brokerlist.id field and a text entered boatname. brokername字段包含brokerlist.id字段的值和一个输入的文本boatName。

I have created php add forms to add data to both tables that are working properly. 我创建了php add表单,将数据添加到两个工作正常的表中。 I have created a php edit form to edit the brokerlist table that works properly. 我创建了一个php编辑表单来编辑可以正常工作的Brokerlist表。 My problem lies in the form I call testeditboat.php. 我的问题在于我称为testeditboat.php的形式。 What I am trying to do is allow the user to change the field brokername in the testeditboat.php form with a dropdown box that populates from the mysql table brokerlist but also uses the select selected of the current entry. 我正在尝试做的是允许用户使用从MySQL表代理列表填充的下拉框来更改testitboat.php表单中的字段brokername,但也使用当前条目的select选择。

Where my form currently stands is it submits the request and will change any value in the boatname field, but will not update the brokername field. 我的表单当前所在的位置是它提交请求,并且将更改boatname字段中的任何值,但不会更新brokername字段。

Thank you all in advance very much for your help, I hope I have been descriptive enough. 预先非常感谢大家的帮助,我希望我已经描述得足够多了。

The code for my testeditboat.php form is as follows: 我testeditboat.php表单的代码如下:

<?php

    include_once('db.php');     

    if(isset($_GET['edit']))

    {

    $id = $_GET['edit'];    
    $res= mysql_query("SELECT * FROM boatlist WHERE id='$id'");    
    $row= mysql_fetch_array($res);   
    $brokerlistquery = mysql_query("SELECT * FROM brokerlist");

    }


    if( isset($_POST['newbrokername']) && isset($_POST['newboatname']))    
       {    
         $newbrokername = $_POST['newbrokername'];
         $newboatname = $_POST['newboatname'];     
         $id= $_POST['id'];

         $sql = "UPDATE boatlist SET brokername='$newbrokername', boatname='$newboatname' WHERE id='$id'";

         $res= mysql_query($sql) or die("Could not update".mysql_error());

         echo "<meta http-equiv='refresh' content='0;url=testaddboat.php'>";

    }



?>

<table class="centerwithroom">    
<form action="testeditboat.php" method="POST">    
<tr>    
<td>New Broker Name </td><td>
<select name='newbrokername'>
  <?php
    while ($brokerlistrow = mysql_fetch_array($brokerlistquery)) {
  ?>
<option value="<?php echo $row[brokername]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
</td>

<tr>
<td>Boat Name:</td><td><input type="text" name="newboatname" value="<?php echo $row[boatname]; ?>"/></td>
</tr>    

</tr>

<td></td><td><input type="submit" value=" Update "/></td>

</tr>

<input type="hidden" name="id" value="<?php echo $row[id]; ?>"/>    

</form>
</table>

Change the following line: 更改以下行:

$row= mysql_fetch_assoc($res); //this fetches the array in associative mode

Change the following line: 更改以下行:

<option value="<?php echo $row[brokername]; ?>" <?php if ($row[brokername] == $brokerlistrow[name]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>

You where comparing $row[brokername] == $brokerlistrow[id] which seems to be incorrect. 您在哪里比较$row[brokername] == $brokerlistrow[id] ,这似乎是不正确的。 You have to compare $row[brokername] == $brokerlistrow[name] 您必须比较$row[brokername] == $brokerlistrow[name]

Note: Stop using mysql_* extensions use mysqli_ instead* 注意:停止使用mysql_ *扩展名,改用mysqli_ *

by changing the row to 通过将行更改为

<option value="<?php echo $brokerlistrow[id]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>

it now saved the correct value and shows the correct focus in the edit dropdown box. 现在,它保存了正确的值,并在编辑下拉框中显示了正确的焦点。

Thank you all for your guidance! 谢谢大家的指导! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM