[英]Record can't delete in PHP
我目前正在研究食品餐厅系统。
我是 PHP 的初学者。 我在从数据库中删除记录时遇到了一点问题。当我尝试删除记录时,它会取消链接图像,但不会删除行。 我该如何解决?
任何帮助,将不胜感激。 谢谢你。
这是我的脚本::
产品列表.php
<?php
$pd = new Product();
$fm = new Format();
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])){
$insertProduct = $pd->productInsert($_POST,$_FILES);
}
if(isset($_GET['delpro'])){
$id = $_GET['delpro'];
$delpro = $pd->delProById($id);
}
?>
<div class="grid_10">
<div class="box round first grid">
<h2>Post List</h2>
<div class="block">
<table class="data display datatable" id="example">
<tbody>
<?php
$getPd = $pd->getAllProduct();
if($getPd){
$i = 0;
while($result = $getPd->fetch_assoc()){
$i++;
?>
<tr class="odd gradeX">
<td><?php echo $i;?></td>
<td><?php echo $result['productName'];?></td>
<td><?php echo $result['catName'];?></td>
<td><?php echo $result['brandName'];?></td>
<td><?php echo $fm->textShorten($result['body'],50);?></td>
<td><?php echo $result['price'];?></td>
<td><img src="<?php echo $result['image']; ?>" height="40px" width="60px"/></td>
<td>
<?php
if($result['type'] == 0){
echo "Featured";
} else{
echo "General";
}
?></td>
<td><a href="productedit.php?proid=<?php echo $result['productId'];?>">Edit</a> || <a onclick="return confirm('Are you sure to delete!')" href="?delpro=<?php echo $result['productId'];?>">Delete</a></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
产品.php
public function delProById($id){
$query = "SELECT * FROM tbl_product WHERE productId = '$id'";
$getData = $this->db->select($query);
if($getData){
while($delImg = $getData->fetch_assoc()){
$dellink = $delImg['image'];
unlink($dellink);
}
}
$delquery = "DELETE FROM tbl_product WHERE productId = '$id'";
$delData = $this->db->delete($query);
if($delData){
$msg = "<span class='success'>Product Deleted Successfully</span>";
return $msg;
}
else {
$msg = "<span class='error'>Product Not Deleted.</span>";
return $msg;
}
}
您正在使用错误的查询执行删除语句。 您应该执行以下操作,而不是包含 SELECT 语句的 $query:
$delData = $this->db->delete($delquery);
我不知道您使用的是什么框架(看起来像 CodeIgniter 左右)但显然您正在尝试运行错误的查询。 尝试:
$delData = $this->db->query($delQuery)
代替
$delData = $this->db->delete($query)
如果 PHP 框架确实是 CodeIgniter (或任何框架),则强烈建议不要在 Z2FEC392304A7F9B37AC1 中运行原始 sql 请参考: CodeIgniter 查询生成器 Class
MySQLi中没有->delete function。 你将只使用
$this->db->query($query);
SQL 中的每个命令都在该行中。 对 SQL object 的调用只是内部数据库的代理。 SQL 的命令库是内部的。 所以我们只使用变量,即 object,现在连接后,我们删除查询本身带来的记录。
我假设您正在使用一些 class 或管理 SQL 查询的框架来使用 select 和删除方法。
只需更改查询变量名称: $delData = $this->db->delete( $query );
到:$delData = $this->db->delete( $delquery );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.