2015-12-16 19 views
1

我正在用單選按鈕和下拉菜單製作一個簡單的註冊表單。使用下拉菜單捕獲數據,mysql中的單選按鈕

我的問題是我無法將表單數據插入到我的數據庫中。我認爲我沒有捕獲表單數據,但我不確定。 因爲當我提交表單我得到的錯誤SORRY! ERROR while inserting record !這是告訴我它無法insert.i嘗試添加

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

讓我看到我的顯示頁面上的實際錯誤,但我仍然無法看到弄清楚我做錯了什麼。

頁代碼:

  <?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'; ?> 

我的CRUD功能

<?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 */ 

} 
+0

可能的重複http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index –

+0

http://php.net/manual/en/pdo.error- handling.php --- http://php.net/manual/en/function.error-reporting.php得到真正的錯誤,爲什麼它失敗。 –

+0

你能給我們提供具體的錯誤嗎? – BryanLavinParmenter

回答

0

你的問題是你沒有正確編碼你的HTML中的<input>標籤。

每個<input>標籤需要name=""屬性,因爲它是這個屬性,而不是瀏覽器用來創建name=value雙被髮送作爲$_GET$_POST變量

所以修改你的代碼,並添加name=""屬性id=""ロ所有輸入標籤,如下所示: -

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

等所有輸入字段,確保你給每個輸入的名稱相匹配您使用的PHP代碼的名稱。

還要確保name="last_name"與$ _POST ['last_name']相同。

案件也應該匹配,所以name='Employee NRC'$_POST['employee_nrc'];不匹配,不會工作。也不要在name='Employee NRC'中輸入空格,或者像name='employee_nrc'那樣刪除空格或使用下劃線_

+0

我已經試過這樣做,但我仍然無法插入數據,我已編輯我的問題,請檢查我是否仍然缺少一些東西 – bli

+0

請參閱我的答案中的其他信息。 – RiggsFolly

+0

完成但仍然得到相同的錯誤響應,我無法插入 – bli

-1

嘗試把輸入名稱=「...」到你的表單字段,你目前有沒有參考$後變量。

+0

真的不知道這是如何downvoted,因爲它提供了一個答案? – Juakali92