我有以下代码在网页上列出完整的公司列表。
$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 );
}
但是我无法工作,任何人都有更好的办法吗?
答案 0 :(得分:0)
请将您的代码与我的代码进行比较,您的版本中存在一些错误或误解。
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...
个标签
答案 1 :(得分:0)
试试这个:
$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'])){
OR
此img\binIcon.png
应为此img/binIcon.png
无论如何,请尝试我的代码,让我知道这是怎么回事。