删除链接无法获取要删除的项目的确切ID

时间:2017-03-02 05:32:26

标签: php html mysql

当我的编码没有设法选择要删除的项目的ID时,我不能得到它,我认为应该获得正确的ID。我使用下面的编码显示了我的所有项目。显示的每个项目都有自己的删除修改链接。

<?php           
    $sql_select = "SELECT * FROM tblproduct LEFT JOIN productcategory ON tblproduct.prodCat = productcategory.catID ORDER BY tblproduct.id DESC";
    $result = $conn->query($sql_select);

    if ($result->num_rows > 0){
        while($row = mysqli_fetch_assoc($result))
        {
            ?>
            <li>
                <h3><?php echo $row['prodName']; ?></h3>
                <div>
                    <img src="<?php echo "user_images/".$row['prodImg']; ?>" />
                </div>
                <p>Code : <?php echo $row['prodCode']; ?></p>
                <p>Price : RM<?php echo $row['prodPrice']; ?></p>
                <p>Category : <?php echo $row['prodCat']; ?></p>
                <p>Description : <?php echo $row['prodDesc']; ?></p>
                <span>
                    <a class="btn btn-info" href="editform.php?edit_id=<?php echo $row['id']; ?>" title="click for edit" onclick="return confirm('sure to edit ?')"><span class="glyphicon glyphicon-edit"></span> Edit</a> 
                    <a class="btn btn-danger" href="function.php?delete_id=<?php echo $row['id']; ?>" title="click for delete" onclick="return confirm('sure to delete ?')"><span class="glyphicon glyphicon-remove-circle"></span> Delete</a>
                </span>
            </li>
            <?php
        }
    }
?>  

请参阅上面的代码,当我点击删除链接时,商品ID始终返回1,而我的数据库只有两个记录,其ID从17

1 个答案:

答案 0 :(得分:5)

列ID不明确,也可以来自 productcategory 。所以你需要做类似下面的事情

$sql_select = "SELECT tblproduct.id as product_id,* FROM tblproduct LEFT JOIN productcategory ON tblproduct.prodCat = productcategory.catID ORDER BY tblproduct.id DESC";

HTML

<a class="btn btn-danger" href="function.php?delete_id=<?php echo $row['product_id']; ?>" title="click for delete" onclick="return confirm('sure to delete ?')"><span class="glyphicon glyphicon-remove-circle"></span> Delete</a>
 </span>