简体   繁体   中英

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. 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

 <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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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