从 PHP 的 MSSQL table 中删除一行

Delete a single row from an MSSQL table from PHP

我有以下代码可以在网页上列出完整的公司列表。

$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ){
    echo $row['company']
}

我想要做的是在每个公司名称旁边都有一个图标,以便我可以在需要时删除它们。我尝试使用以下方法实现此目的:

$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
$id = $row['id'];
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ){
    echo $row['company']. "<input type='image' name='". $id ."' 
src='img\binIcon.png'/></br>" ;
}
    if(isset($_POST[$id])){
    $delCompany = "DELETE FROM dbo.Companies WHERE id = $id";
    $stmt = sqlsrv_query( $conn, $delCompany );
}

但是我无法开始工作,有人有更好的方法吗?

请将你的代码与我的进行比较,你的版本中有一些错误或误解..

    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ){
        echo $row['company']. "<input type='image' name='id' value='". $row['id'] ."' 
    src='img\binIcon.png'/></br>" ;
    }
        if(isset($_POST['id'])){
        $delCompany = "DELETE FROM dbo.Companies WHERE id = '".intval($_POST['id'])."'";
        $stmt = sqlsrv_query( $conn, $delCompany );

}

不用说你的输入周围应该有 <form... 标签

试试这个:

$query = "SELECT `id`, `company` FROM `YOUR TABLE` ORDER BY id";

$products = "";
if ($stmt = mysqli_prepare($conn, $query)) {

    /* execute statement */
 mysqli_stmt_execute($stmt);

    /* bind result variables */
 mysqli_stmt_bind_result($stmt, $id, $company);

    /* fetch values */
 while (mysqli_stmt_fetch($stmt)) {
 $products .= "<a href='your-page.php?id=". $id ."'>".$company." | <img src='img/binIcon.png'/></a>";

    }
} 

////PRINT OUT ALL YOUR ITEMS HERE//////
echo $products;



////Delete the item//////
///Its always a good idea to double check and ask the user if they are sure that they want to delete the row in case they pressed the delete button by accident///

if(isset($_GET['id'])){
$Did = $_GET['id'];
    $delCompany = "DELETE FROM dbo.Companies WHERE id='$Did'";
    $stmt = sqlsrv_query( $conn, $delCompany );
}

您的代码中有一些错误!

例如if(isset($_POST[$id])){我想你是想写:if(isset($_POST['id'])){

这个img\binIcon.png应该是这个img/binIcon.png

无论如何,试试我的代码,让我知道结果如何。