简体   繁体   English

如何编辑下拉选择的值?

[英]How to edit drop-down selected value?

I have created a drop-down menu and many other text fields on my html page which fetches the option values from database. 我在html页面上创建了一个下拉菜单和许多其他文本字段,可从数据库中获取选项值。 It is inserting the selected values in my database table. 它将选定的值插入我的数据库表中。 I want to edit the selected values from the drop-down menu and update them in database. 我想从下拉菜单中编辑选定的值,然后在数据库中更新它们。 When I open the edit page, the form shows the previous saved values to edit them. 当我打开编辑页面时,该表单显示先前保存的值以对其进行编辑。 But the problem is; 但是问题是; When I don't edit the drop-down values and submit the form, It shows me error of undefined index and when I edit or select different value of drop-down then it works fine and don't show any error. 当我不编辑下拉列表的值并提交表单时,它向我显示未定义索引的错误;当我编辑或选择不同的下拉列表的值时,它可以正常工作,并且不显示任何错误。 Here is my code: HTML 这是我的代码: HTML

 <label>Courses</label><select name="courses" id="courses" class="dropdownclass"><option selected="selected" value="" disabled selected hidden>-- Select an option --</option><?php mysql_connect('localhost', 'root', ''); mysql_select_db('db'); $sql = "SELECT courses FROM table"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { echo "<option value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>"; } ?> </select> <!-- begin snippet: js hide: false console: true babel: false --> 

EDITRECORD 编辑记录

 <?php include("connection.php"); $Sno = (int)$_GET['Sno']; $query = mysql_query("SELECT * FROM table WHERE Sno = '$Sno'") or die(mysql_error()); while($row = mysql_fetch_array($query)) { if (isset($_POST)) { echo ""; $id=$row['id']; $courses=$row['courses']; } } ?> <!DOCTYPE html> <html> <head> <title>Edit Record</title> </head> <body> <form id="form" action="update.php" method="post" enctype="multipart/form-data"> <fieldset> <input type="hidden" name="new" id="Sno" value="<?=$Sno;?>" /> <label>Courses</label><select name="courses" id="courses" class="dropdownclass" ><option selected="selected" value="" disabled selected hidden><?php echo $courses; ?></option><?php mysql_connect('localhost', 'root', ''); mysql_select_db('db'); $sql = "SELECT courses FROM table"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { echo "<option value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>"; } ?> </select> </fieldset> </form> </body> </html> 

UPDATE 更新

 <?php include("connection.php"); $Sno =''; if( isset( $_POST['new'])) { $Sno = (int)$_POST['new']; } $id = mysql_real_escape_string($_POST['id']); if(isset($_POST['courses'])){ $courses = mysql_real_escape_string($_POST['courses']); }else{ $courses=$_POST['courses']; } query="UPDATE technicalsol SET id= '$id', courses = '$courses' WHERE Sno=$Sno"; $res= mysql_query($query); if($res){ echo "<div style ='font-size:20px; margin-left:140px;'>Records updated Successfully</div>"; include("search.php"); }else{ echo "Problem updating record. MY SQL Error: " . mysql_error(); } ?> 

I want that; 我要那个; If I don't edit the drop-down selected value, It just takes the previous value and saves it. 如果我不编辑下拉选择的值,它将仅使用先前的值并保存。

What you are doing right now is listing the options in dropdown list. 您现在正在做的是在下拉列表中列出选项。 You are not setting up any answer 您没有设置任何答案

In your HTML: 在您的HTML中:

while ($row = mysql_fetch_array($result)) {

        echo "<option value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
    }

make sure you make the old value selected. 确保您选择了旧值。 may be by using an IF statement 可能是通过使用IF语句

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

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