我正在使用单选按钮和下拉菜单制作一个简单的注册表单。
我的问题是我无法将表单数据插入到我的数据库中。我想我不是在捕捉表单数据,但我不确定。
因为当我提交表单时,我收到错误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> 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 :(得分:0)
您的问题是您没有正确编码HTML中的k
代码。
每个<input>
标记都需要<input>
属性,因为它是此属性,而不是浏览器用来创建name=""
对的id=""
name=value
1}}或$_GET
变量
所以修改你的代码并在所有输入标签中添加$_POST
属性,如下所示: -
name=""
所有输入字段的等等,确保您为每个输入提供的名称与您在PHP代码中使用的名称相匹配。
还要确保<input type="text" name="first_name" class="form-control" id="inputFirstName" placeholder="first name">
与$ _POST ['last_name']相同。
大小写也应该匹配,因此name="last_name"
和name='Employee NRC'
不匹配且无法正常工作。也不要在$_POST['employee_nrc'];
中添加空格,删除空格,或者在name='Employee NRC'
这样的名称中使用下划线_
。