我试图弄清楚我从这个论坛得到的这个代码有什么问题基本上我试图修改它,看看我是否可以解决上面的错误,这也带来了一个未定义的索引错误。我的问题是错误的原因,因为所有错误似乎都很好,绑定是好的
如果我将POST
替换为GET
中的if(isset($_POST['btn-update']))
,则错误消失但没有发生任何事情。我试图了解上述sqlstate[hy093] error
和{{1}的原因在undefined index error
上,在crud函数中定义
$id = $_POST['edit_id'];
crud类中的绑定似乎没问题
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;
}
发生未定义索引错误的编辑页面为<?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, business_type ,business_site ,business_activity) 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,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivityt)
{
try
{
$stmt=$this->db->prepare("UPDATE tbl_users SET first_name=:fname,
last_name=:lname,
employee_nrc=:employee_nrc,
phone_no=:Phone,
business_type=:Businesstype,
business_site=:Businesssite,
business_activity=: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['business_type']); ?></td>
<td><?php print($row['business_site']); ?></td>
<td><?php print($row['business_activity']); ?></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 */
}
$id = $_POST['edit_id'];
帮助识别出于学习目的的错误和建议,我会很感激想法和解决方案
答案 0 :(得分:0)
有一种类型后来在此代码上进行了更正
$stmt->bindparam(":Businesstype",$Phone);
应该是
$stmt->bindparam(":Phone",$Phone);
感谢。