Php 使用mysql中的下拉菜单和单选按钮捕获数据

Php 使用mysql中的下拉菜单和单选按钮捕获数据,php,html,mysql,pdo,Php,Html,Mysql,Pdo,我正在用单选按钮和下拉菜单制作一个简单的登记表 我的问题是无法将表单数据插入数据库。我想我没有捕获表单数据,但我不确定。 因为当我提交表单时,我得到了错误对不起!插入记录时出错它告诉我插入失败。我已尝试添加 ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL); 因此,我可以看到我的页面上显示的实际错误,但我仍然无法看到,以找出我做错了什么 页面代码:

我正在用单选按钮和下拉菜单制作一个简单的登记表

我的问题是无法将表单数据插入数据库。我想我没有捕获表单数据,但我不确定。 因为当我提交表单时,我得到了错误
对不起!插入记录时出错它告诉我插入失败。我已尝试添加

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

记录已成功插入!
对不起插入记录时出错!

登记制度 名字 姓 雇员核管理委员会 员工电话号码 业务类型 宅地摊位 出售 营业地点 市场 居住区 街头贩卖 商业活动 农业、林业和渔业 采矿和采石 制造业 电力、燃气、蒸汽和空调供应 添加记录
我的积垢函数

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

}

这里什么都没有。。。

    您的问题是没有在HTML中正确编码
    标记

    每个
    标记都需要一个
    name=”“
    属性,因为它是该属性,而不是浏览器用来创建
    name=value
    对的
    $\u GET
    $\u POST
    变量

    因此,请修改您的代码并在所有输入标记中添加
    name=“
    属性,如下所示:-

    <input type="text" name="first_name" class="form-control" id="inputFirstName"  placeholder="first  name">
    
    
    
    以此类推,确保为每个输入指定的名称与PHP代码中使用的名称相匹配

    还要确保
    name=“last\u name”
    与$\u POST['last\u name']相同


    案例也应该匹配,因此
    name='Employee NRC'
    $\u POST['Employee\u NRC']不匹配且无法工作。也不要在
    name='Employee NRC'
    中添加空格,或者删除空格,或者在名称中添加下划线
    \uu
    ,例如
    name='Employee\u NRC'
    您的问题是没有在HTML中正确编码
    标记

    每个
    标记都需要一个
    name=”“
    属性,因为它是该属性,而不是浏览器用来创建
    name=value
    对的
    $\u GET
    $\u POST
    变量

    因此,请修改您的代码并在所有输入标记中添加
    name=“
    属性,如下所示:-

    <input type="text" name="first_name" class="form-control" id="inputFirstName"  placeholder="first  name">
    
    
    
    以此类推,确保为每个输入指定的名称与PHP代码中使用的名称相匹配

    还要确保
    name=“last\u name”
    与$\u POST['last\u name']相同


    案例也应该匹配,因此
    name='Employee NRC'
    $\u POST['Employee\u NRC']不匹配且无法工作。也不要在
    name='Employee\u NRC'
    中添加空格,或者在名称中删除空格或下划线
    \u
    ,例如
    name='Employee\u NRC'

    可能的重复---以获取失败原因的真实错误。您能告诉我们具体错误吗?我得到的错误是我自己的错误,我在代码i中输入的。对不起!插入记录时出错@bli您的create方法有一行
    echo$e->getMessage()-不要让您的代码重定向,这样会删除消息。阅读信息。抱歉,错误不是一条有意义的编程消息,我们无法调试它。可能重复的-,以获取失败原因的真实错误。您能告诉我们具体错误吗?我得到的错误是我自己的错误,我在代码i中输入了错误。对不起!插入记录时出错@bli您的create方法有一行
    echo$e->getMessage()-不要让您的代码重定向,这样会删除消息。阅读信息。抱歉,此错误不是有意义的编程消息,我们无法调试。我已尝试过这样做,但仍然无法插入数据,我已经编辑了我的问题,请检查我是否仍然遗漏了一些内容。请查看我答案中的其他信息。完成后,我仍然无法插入相同的错误响应。您最好再次更新问题中的代码
    $\u POST['employee\u nrc']不等于
    name=“Employee\u nrc”
    案例也必须相同。请检查是否存在其他不匹配。我已经尝试过这样做,但是我仍然无法插入数据,我已经编辑了我的问题。请检查我是否仍然缺少一些内容。请查看我的答案中的其他信息。完成后,仍然得到相同的错误响应我无法插入您最好再次更新问题中的代码
    $\u POST['employee\u nrc'];不等于
    name=“Employee\u nrc”
    案例也必须相同。请检查是否存在其他不匹配