0
votes

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.

1
the problem you are having is when you do not edit and submit right? This gives you an undefined index error? - Daniel H.
Don't use the deprecated and insecure mysql_*-functions. They have been deprecated since PHP 5.5 (in 2013) and were completely removed in PHP 7 (in 2015). Use MySQLi or PDO instead. - Magnus Eriksson
Also, this: $courses=$_POST['courses']; doesn't make sense since you're doing it if $_POST['courses'] isn't set. - Magnus Eriksson

1 Answers

-1
votes

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