blueguy777 0 Newbie Poster

Update form is working perfectly, but the value of $magentname variable changing to blank without selecting drop down box value.

code:

<?php 

    require 'include/database.php';
    require 'include/settings.php';
    $id = null;
    if ( !empty($_GET['id'])) {
        $id = $_REQUEST['id'];
    }

    if ( null==$id ) {
        header("Location: sub-agents-list.php");
    }       
        $mainagentid = $_POST["mainagentid"];
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM agentreg where agentid = ?";
        $q = $pdo->prepare($sql);
        $q->execute(array($mainagentid));
        $data = $q->fetch(PDO::FETCH_ASSOC);
        $magentid=$data['agentid'];
        $magentname=$data['firstname']."  ".$data['lastname'];      
        Database::disconnect();

    if ( !empty($_POST)) {
        // keep track validation errors
        $firstnameError = null;
        $lastnameError = null;
        $cityError = null;      
        $mobno1Error = null;        
        $mainagentidError = null;               


        // keep track post values

        $agentid = $_POST['agentid'];
        $firstname = str_replace(',', '' , $_POST['firstname']);
        $lastname = str_replace(',', '' , $_POST['lastname']);
        $address = $_POST['address'];                               
        $city = $_POST['city'];                                     
        $mobno1 = $_POST['mobno1'];     
        $mobno2 = $_POST['mobno2'];             
        $skimcity = $_POST['skimcity'];                                     
        $skimyear = $_POST['skimyear'];     
        $skimname = $_POST['skimname'];             

        $weight=0; 

        // validate input

        $valid = true;
        if (empty($firstname)) {
            $firstnameError = 'Please enter First Name';
            $valid = false;
        } 

        if (empty($lastname)) {
            $lastnameError = 'Please enter Last Name';
            $valid = false;
        }

        if (empty($city)) {
            $cityError = 'Please enter City';
            $valid = false;
        }


        if (empty($mobno1)) {
            $mobno1Error = 'Please enter Mobile Number 1';
            $valid = false;
        }

        elseif (!preg_match("/^\d{10}$/", $mobno1)) {
            $mobno1Error = 'Please enter 10 digit Mobile Number';
            $valid = false;
        }



        // update data
        if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "UPDATE agentreg set firstname = ?, lastname = ?, address = ?, city = ?, mobno1 = ?, mobno2 = ?, magentid = ?, magentname = ? WHERE id = ?";
            $q = $pdo->prepare($sql);
            $q->execute(array($firstname,$lastname,$address,$city,$mobno1,$mobno2,$magentid,$magentname,$id));
            Database::disconnect();

//agent parent table 
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "UPDATE agentparent SET agentid = ?, parent = ?";
            $q = $pdo->prepare($sql);
            $q->execute(array($agentid,$magentid));
            Database::disconnect();
            header("Location: sub-agents-list.php");
        }
    }
else {
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM agentreg where id = ?";
        $q = $pdo->prepare($sql);
        $q->execute(array($id));
        $data = $q->fetch(PDO::FETCH_ASSOC);
        $agentid = $data['agentid'];
        $firstname = $data['firstname'];
        $lastname = $data['lastname'];
        $address = $data['address'];
        $city = $data['city'];
        $mobno1 = $data['mobno1'];
        $mobno2 = $data['mobno2'];
        $magentname = $data['magentname'];      

        Database::disconnect();
    }   
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">

                <div class="span10 offset1">
            <div class="row">
                <p><br />
                    <a href="update-sub-agent.php" class="btn btn-success">Edit Sub Agents</a>
                </p>
        </div>            

                    <div class="row">
                        <h3>Sub Agent Registration </h3>
                    </div>
                    <form class="form-horizontal" action="update-sub-agent.php?id=<?php echo $id?>" method="post">
                      <div class="control-group">
                        <label class="control-label">Agent ID</label>
                        <div class="controls">
                            <?php echo $agentid; ?>
                        </div>
                      </div>
                      <div class="control-group <?php echo !empty($firstnameError)?'error':'';?>">
                        <label class="control-label">First Name</label>
                        <div class="controls">
                            <input name="firstname" type="text" placeholder="First Name" value="<?php echo !empty($firstname)?$firstname:'';?>">
                            <?php if (!empty($firstnameError)): ?>
                                <span class="help-inline"><?php echo $firstnameError;?></span>
                            <?php endif;?>
                        </div>
                      </div>
                      <div class="control-group <?php echo !empty($lastnameError)?'error':'';?>">
                        <label class="control-label">Last Name</label>
                        <div class="controls">
                            <input name="lastname" type="text"  placeholder="Last Name" value="<?php echo !empty($lastname)?$lastname:'';?>">
                            <?php if (!empty($lastnameError)): ?>
                                <span class="help-inline"><?php echo $lastnameError;?></span>
                            <?php endif;?>
                        </div>
                      </div>
                      <div class="control-group ">                      
                        <label class="control-label">Address</label>
                        <div class="controls">
                            <textarea name="address" type="text" placeholder="Address" value="<?php echo !empty($address)?$address:'';?>"><?php echo $address; ?></textarea>
                            <?php if (!empty($addressError)): ?>
                                <span class="help-inline"><?php echo $addressError;?></span>
                            <?php endif;?>
                        </div>
                      </div>

                      <div class="control-group <?php echo !empty($cityError)?'error':'';?>">
                        <label class="control-label">City</label>
                        <div class="controls">
                            <input name="city" type="text"  placeholder="City" value="<?php echo !empty($city)?$city:'';?>">
                            <?php if (!empty($cityError)): ?>
                                <span class="help-inline"><?php echo $cityError;?></span>
                            <?php endif;?>
                        </div>
                      </div>

                      <div class="control-group <?php echo !empty($mobno1Error)?'error':'';?>">
                        <label class="control-label">Mobile Number 1</label>
                        <div class="controls">
                            <input name="mobno1" type="text" placeholder="Mobile Number 1" value="<?php echo !empty($mobno1)?$mobno1:'';?>">
                            <?php if (!empty($mobno1Error)): ?>
                                <span class="help-inline"><?php echo $mobno1Error;?></span>
                            <?php endif;?>
                        </div>
                      </div>
                      <div class="control-group">
                        <label class="control-label">Mobile Number 2</label>
                        <div class="controls">
                            <input name="mobno2" type="text" placeholder="Mobile Number 2" value="<?php echo !empty($mobno2)?$mobno2:'';?>">
                            <?php if (!empty($mobno2Error)): ?>
                                <span class="help-inline"><?php echo $mobno2Error;?></span>
                            <?php endif;?>
                        </div>
                        </div>

                      <div class="control-group">
                        <?php echo $magentname; ?>
                        <label class="control-label">Select Main Agent</label>
                        <div class="controls">
                        <select name="mainagentid"> 

                        <option  value="<?php echo !empty($mainagentid)?$mainagentid:'';?>" selected="selected">Select</option>
<?php 
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM agentreg where weight = 1";
        $q = $pdo->prepare($sql);
        $q->execute();
        $data = $q->fetchALL(PDO::FETCH_ASSOC);
        Database::disconnect();
foreach($data as $row)
{
echo '<option value="'.$row['agentid'].'">'.$row['firstname'].' '.$row['lastname'].'</option>';
}       echo '</select>'; ?>
                            <?php if (!empty($mainagentidError)): ?>
                                <span class="help-inline"><?php echo $mainagentidError;?></span>
                            <?php endif;?>

                        </div>
                      </div>


                      <div class="control-group">
                        <label class="control-label">Scheme City</label>
                        <div class="controls">
                            <?php echo $skimcity; ?>
                            <input name="skimcity" type="hidden" value="<?php echo $skimcity;?>">                                                        
                        </div>
                      </div>

                      <div class="control-group">
                        <label class="control-label">Scheme Year</label>
                        <div class="controls">
                            <?php echo $skimyear; ?>
                            <input name="skimyear" type="hidden" value="<?php echo $skimyear;?>">                                                                                    
                        </div>
                      </div>

                      <div class="control-group">
                        <label class="control-label">Scheme Name</label>
                        <div class="controls">
                            <?php echo $skimname; ?>
                            <input name="skimname" type="hidden" value="<?php echo $skimname;?>">                                                                                    
                        </div>
                      </div>

                      <div class="form-actions">
                          <button type="submit" class="btn btn-success">Update</button>
                          <a class="btn" href="sub-agents-list.php">Back</a>
                        </div>
                    </form>
                </div>

    </div> <!-- /container -->
  </body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.