1

I am making a simple registration form with radio button and a drop-down menu.

My problem is I cannot insert form data into my database. I think I am not capturing the form data but I am not sure. Because when I submit the form I get the error SORRY! ERROR while inserting record ! which is telling me it failed to insert.i have tried to add

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

so that i see the actual errors displayed on my page but i still can not see to figure out what is it that i'm doing wrong.

Page code:

         <?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);


include_once 'dbconfig.php';
if(isset($_POST['btn-save']))
{
    $fname = $_POST['first_name'];
    $lname = $_POST['last_name'];
    $employee_nrc = $_POST['employee_nrc'];
    $Phone = $_POST['phone_no'];
    $Businesstype = $_POST['business_type'];
    $Businesssite = $_POST['business_site'];
    $Businessactivity = $_POST['business_activity'];
    if($crud->create($fname,$lname,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivity))
    {
        header("Location: add-data.php?inserted");
    }
    else
    {
        header("Location: add-data.php?failure");
    }
}
?>
<?php include_once 'header.php'; ?>
<div class="clearfix"></div>

<?php
if(isset($_GET['inserted']))
{
    ?>
    <div class="container">
    <div class="alert alert-info">
    <strong>WOW!</strong> Record was inserted successfully <a href="index.php">HOME</a>!
    </div>
    </div>
    <?php
}
else if(isset($_GET['failure']))
{
    ?>
    <div class="container">
    <div class="alert alert-warning">
    <strong>SORRY!</strong> ERROR while inserting record !
    </div>
    </div>
    <?php
}
?>

<div class="clearfix"></div><br />

<div class="container">

    <form class="form-horizontal" method='post'>
  <fieldset>
    <legend>Registration System</legend>
    <div class="form-group">
      <label for="inputFirstName" class="col-lg-2 control-label">First Name</label>
      <div class="col-lg-10">
        <input type="text" name="first_name" class="form-control" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputLastName" class="col-lg-2 control-label">Last Name</label>
      <div class="col-lg-10">
        <input type="text" name="last_name" class="form-control" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputEmployeeNRC" class="col-lg-2 control-label">Employee NRC</label>
      <div class="col-lg-10">
        <input type="text" name="Employee_nrc" class="form-control" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Employee Phone Number</label>
      <div class="col-lg-10">
        <input type="text" name="phone_no" class='form-control' required>
      </div> 
    </div>


    <div class="form-group">
      <label class="col-lg-2 control-label">Business Type</label>
      <div class="col-lg-10">

         <div class="radio">
          <label>
            <input type="radio" name="business_type" value=" HomeStead stalls" checked>
            HomeStead stalls
          </label>
        </div>
         <div class="radio">
          <label>
            <input type="radio" name="business_type" value=" Vending" checked>
            Vending
          </label>
        </div>

      </div>
    </div>

    <div class="form-group">
      <label class="col-lg-2 control-label">Business Location</label>
      <div class="col-lg-10">
        <div class="radio">
          <label>
            <input type="radio" name="business_site" value=" Market" checked>
            Market
          </label>
        </div>
        <div class="radio">
          <label>
            <input type="radio" name="business_site" value="Residential Area" checked>
            Residential Area
          </label>
        </div>

        <div class="radio">
          <label>
            <input type="radio" name="business_site" value="Street Vending" checked>
            Street Vending
          </label>
        </div>

      </div>
    </div>
    <div class="form-group">
      <label for="select" class="col-lg-2 control-label">Business Activities</label>
      <div class="col-lg-10">
        <select class="form-control" name="business_activity">

          <option value="Agriculture, forestry and fishing">Agriculture, forestry and fishing</option>
          <option value="Mining and quarrying">Mining and quarrying</option>
          <option value="Manufacturing">Manufacturing</option>
          <option value="Electricity, gas, steam and air conditioning supply">Electricity, gas, steam and air conditioning supply</option>


        </select>

      </div>
    </div>
    <div class="form-group">
      <div class="col-lg-10 col-lg-offset-2">
        <button type="submit" class="btn btn-primary" name="btn-save">
            <span class="glyphicon glyphicon-plus"></span> ADD Record
            </button>  
            <a href="index.php" class="btn btn-large btn-success"><i class="glyphicon glyphicon-backward"></i> &nbsp; Back to index</a>
      </div>
    </div>
  </fieldset>
</form>



</div>

<?php include_once 'footer.php'; ?>

my crud function

<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

class crud
{
    private $db;

    function __construct($DB_con)
    {
        $this->db = $DB_con;
    }

    public function create($fname,$lname,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivity)
    {
        try
        {
            $stmt = $this->db->prepare("INSERT INTO tbl_users(first_name,last_name,employee_nrc, phone_no, Businesstype ,Businesssite ,Businessactivity) VALUES(:fname, :lname, :employee_nrc, :Phone, :Businesstype, :Businesssite,   :Businessactivity)");
            $stmt->bindparam(":fname",$fname);
            $stmt->bindparam(":lname",$lname);
            $stmt->bindparam(":employee_nrc",$employee_nrc);
            $stmt->bindparam(":Phone",$Phone);
            $stmt->bindparam(":Businesstype",$Businesstype);
            $stmt->bindparam(":Businesssite",$Businesssite);
            $stmt->bindparam(":Businessactivity",$Businessactivity);
            $stmt->execute();
            return true;
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();  
            return false;
        }

    }

    public function getID($id)
    {
        $stmt = $this->db->prepare("SELECT * FROM tbl_users WHERE id=:id");
        $stmt->execute(array(":id"=>$id));
        $editRow=$stmt->fetch(PDO::FETCH_ASSOC);
        return $editRow;
    }

    public function update($id,$fname,$lname,$email,$contact)
    {
        try
        {
            $stmt=$this->db->prepare("UPDATE tbl_users SET first_name=:fname, 
                                                       last_name=:lname, 
                                                       employee_nrc=:employee_nrc,
                                                       phone_no=:Phone,
                                                       Businesstype=:Businesstype,
                                                       Businesssite=:Businesssite,
                                                       Businessactivity=:Businessactivity
                                                    WHERE id=:id ");
            $stmt->bindparam(":fname",$fname);
            $stmt->bindparam(":lname",$lname);
            $stmt->bindparam(":employee_nrc",$employee_nrc);
            $stmt->bindparam(":Businesstype",$Phone);
            $stmt->bindparam(":Businesstype",$Businesstype);
            $stmt->bindparam(":Businesssite",$Businesssite);
            $stmt->bindparam(":Businessactivity",$Businessactivity);
            $stmt->bindparam(":id",$id);
            $stmt->execute();

            return true;    
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();  
            return false;
        }
    }

    public function delete($id)
    {
        $stmt = $this->db->prepare("DELETE FROM tbl_users WHERE id=:id");
        $stmt->bindparam(":id",$id);
        $stmt->execute();
        return true;
    }

    /* paging */

    public function dataview($query)
    {
        $stmt = $this->db->prepare($query);
        $stmt->execute();

        if($stmt->rowCount()>0)
        {
            while($row=$stmt->fetch(PDO::FETCH_ASSOC))
            {
                ?>
                <tr>
                <td><?php print($row['id']); ?></td>
                <td><?php print($row['first_name']); ?></td>
                <td><?php print($row['last_name']); ?></td>
                <td><?php print($row['employee_nrc']); ?></td>
                <td><?php print($row['phone_no']); ?></td>
                <td><?php print($row['Businesstype']); ?></td>
                <td><?php print($row['Businesssite']); ?></td>
                <td><?php print($row['Businessactivity']); ?></td>

                <td align="center">
                <a href="edit-data.php?edit_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-edit"></i></a>
                </td>
                <td align="center">
                <a href="delete.php?delete_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-remove-circle"></i></a>
                </td>
                </tr>
                <?php
            }
        }
        else
        {
            ?>
            <tr>
            <td>Nothing here...</td>
            </tr>
            <?php
        }

    }

    public function paging($query,$records_per_page)
    {
        $starting_position=0;
        if(isset($_GET["page_no"]))
        {
            $starting_position=($_GET["page_no"]-1)*$records_per_page;
        }
        $query2=$query." limit $starting_position,$records_per_page";
        return $query2;
    }

    public function paginglink($query,$records_per_page)
    {

        $self = $_SERVER['PHP_SELF'];

        $stmt = $this->db->prepare($query);
        $stmt->execute();

        $total_no_of_records = $stmt->rowCount();

        if($total_no_of_records > 0)
        {
            ?><ul class="pagination"><?php
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;
            if(isset($_GET["page_no"]))
            {
                $current_page=$_GET["page_no"];
            }
            if($current_page!=1)
            {
                $previous =$current_page-1;
                echo "<li><a href='".$self."?page_no=1'>First</a></li>";
                echo "<li><a href='".$self."?page_no=".$previous."'>Previous</a></li>";
            }
            for($i=1;$i<=$total_no_of_pages;$i++)
            {
                if($i==$current_page)
                {
                    echo "<li><a href='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
                }
                else
                {
                    echo "<li><a href='".$self."?page_no=".$i."'>".$i."</a></li>";
                }
            }
            if($current_page!=$total_no_of_pages)
            {
                $next=$current_page+1;
                echo "<li><a href='".$self."?page_no=".$next."'>Next</a></li>";
                echo "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Last</a></li>";
            }
            ?></ul><?php
        }
    }

    /* paging */

}
bli
  • 79
  • 7
  • possible duplicate of http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – Funk Forty Niner Dec 16 '15 at 23:08
  • http://php.net/manual/en/pdo.error-handling.php --- http://php.net/manual/en/function.error-reporting.php to get the real errors as to why it failed. – Funk Forty Niner Dec 16 '15 at 23:09
  • Can you give us the specific error? – BryanLavinParmenter Dec 16 '15 at 23:10
  • the error im getting is my own error which i put in the code i. e SORRY! ERROR while inserting record ! – bli Dec 16 '15 at 23:15
  • @bli Your create method has the line `echo $e->getMessage();` - do not let your code redirect which erase the message. Read the message. SORRY ERROR is not a meaningful programming message, we can't debug that. – Sheepy Dec 17 '15 at 04:17

1 Answers1

0

Your issue is that you have not properly coded the <input> tags in your HTML.

Each <input> tag required a name="" attribute as it is this attribute and not the id="" that the browser uses to create the name=value pairs that are sent as $_GET or $_POST variables

So amend your code and add a name="" attribute ro all the input tags like so :-

<input type="text" name="first_name" class="form-control" id="inputFirstName"  placeholder="first  name">

and so on for all the input fields, making sure the names you give to each input match the names you are using in your PHP code.

Also make sure that the name="last_name" is the same as the $_POST['last_name'].

Case should also match, so name='Employee NRC' and $_POST['employee_nrc']; do not match and wont work. Also dont put spaces in the name='Employee NRC' either remove spaces or us an underscore _ in the name like name='employee_nrc'.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149