I am new to PDO, i am trying to insert values into database table. but the script is not inserting any values nor displaying error message.help me to fix this issue.

<?php 

    require 'include/database.php';
    require 'include/settings.php';
    if ( !empty($_POST)) {
        // keep track validation errors
        $firstnameError = null;
        $lastnameError = null;
        $cityError = null;      
        $mobno1Error = null;        


        // keep track post values
        $agentid = $_POST['agentid'];
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $address = $_POST['address'];                               
        $city = $_POST['city'];                                     
        $mobno1 = $_POST['mobno1'];     
        $mobno2 = $_POST['mobno2'];             
        $skimcity = $_POST['skimcity'];                                     
        $skimyear = $_POST['skimyear'];     
        $skimname = $_POST['skimname'];             

        $weight=1; 
        $parent=0;

        // validate input

        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;
        }


        // insert data
        if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO agentreg (agentid,firstname,lastname,address,city,mobno1,mobno2,skimcity,skimyear,skimname,weight) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            $q = $pdo->prepare($sql);
            $q->execute(array($agentid,$firstname,$lastname,$address,$city,$mobno1,$mobno2,$skimcity,$skimyear,$skimname,$weight));
            Database::disconnect();
            header("Location: index.php");
        }
    }
?>


<!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">
                        <h3>New Agent Registration </h3>
                    </div>
            <?php
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT id,agentid FROM agentreg ORDER BY id DESC LIMIT 1";
        $q = $pdo->prepare($sql);
        $q->execute(array($agentid));
        $data = $q->fetch(PDO::FETCH_ORI_LAST);
        Database::disconnect();
        $agentid=$data['agentid']+1;
            ?>
                    <form class="form-horizontal" action="register-main-agent.php" method="post">
                      <div class="control-group">
                        <label class="control-label">Card Number</label>
                        <div class="controls">
                            <?php echo $agentid; ?>
                            <input name="agentid" type="hidden" value="<?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:'';?>"></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">
                        <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">Create</button>
                          <a class="btn" href="index.php">Back</a>
                        </div>
                    </form>
                </div>

    </div> <!-- /container -->
  </body>
</html>

Recommended Answers

All 3 Replies

Member Avatar for diafol

Try a more helpful title next time. 95% of threads in this forum could have that.

sorry diafol, i'll

Member Avatar for iamthwee

This is where your debugging skills need to come into play. You may not be able to turn on php errors if this is a production server.

I would start to reduce your code and test each part one by one. Litter it with a generous helping of 'echos' check your db structure as well.

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.