【问题标题】:capturing data using dropdown menu,radio buttons in mysql使用下拉菜单捕获数据,mysql中的单选按钮
【发布时间】:2016-03-23 06:51:20
【问题描述】:

我正在制作一个带有单选按钮和下拉菜单的简单注册表。

我的问题是我无法将表单数据插入我的数据库。我想我没有捕获表单数据,但我不确定。 因为当我提交表单时,我收到错误SORRY! ERROR while inserting record !,它告诉我插入失败。我尝试添加

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

}

【问题讨论】:

标签: php html mysql pdo


【解决方案1】:

您的问题是您没有正确编码 HTML 中的 &lt;input&gt; 标记。

每个&lt;input&gt; 标记都需要一个name="" 属性,因为它是该属性,而不是浏览器用来创建name=value 对的id="",它们作为$_GET$_POST 变量发送

所以修改你的代码并为所有输入标签添加一个name="" 属性,如下所示:-

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

【讨论】:

  • 我已经尝试过这样做,但我仍然无法插入数据,我已经编辑了我的问题,请检查我是否仍然缺少某些东西
  • 在我的回答中查看更多信息。
  • 完成但仍然得到相同的错误响应,我无法插入
  • 您最好再次更新问题中的代码
  • $_POST['employee_nrc']; 不等于name="Employee_nrc" 大小写也必须相同。请检查其他不匹配情况
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 2020-10-18
  • 2013-08-18
  • 2021-09-29
  • 2012-10-27
  • 2021-11-07
相关资源
最近更新 更多