duanjun7801 2016-05-01 14:44
浏览 54

SQL按行删除行

I'm trying to delete a row in an SQL database by an id. I have found questions here related to this but nothing seems to work, perhaps because my page is populated (dynamically?) based on selecting a variable. The rows are displayed on my page based on a dropdown (locationlab) and I have a delete button after each row. It looks like this.

I have the Id displayed temporarily at the end of the row just be sure that the code sees the variable (& it does!).

The code to populate the page looks like this:

<?php
    $locationlab = $_POST[locationlab];
    $sql = "SELECT * FROM lab WHERE locationlab LIKE '{$locationlab}'";
    echo($locationlab);
    $result = $conn->query($sql);
     if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo'
  <table>
  <form action=testpage2.php method=post>
    <td width="10%"><input type=text name=make value='. $row["make"].'></td>
    <td width="10%"><input type=text name=model value='. $row["model"].'></td>
    <td width="20%"><input type=text name=hostname value='. $row["hostname"].'></td>
    <td width="15%"><input type=text name=ipaddress value='. $row["ipaddress"].'></td>
    <td width="20%"><input type=text name=ipmiipaddress value='. $row["ipmiipaddress"].'></td>
    <td width="15%"><input type=text name=terminalserveraddress value='. $row["terminalserveraddress"].'></td>
    <td width="10%"><input type=text name=locationlab value='. $row["locationlab"].'></td>
    <td><input type=submit name=update value=update></td>
    <td><input type=submit name=delete value=delete></td>
    <td id=id name=id value='. $row["id"].'>'. $row["id"].'</td>
</table>
</form>';
        }}
        ?>

I can input the SQL query below manually in the phpMyAdmin page so I know it is correct. The code for the Delete button looks like this:

   <?php
if(isset($_POST['delete'])) {
$deletequery = ("DELETE * FROM lab WHERE ='$_POST[id]'");
mysql_query($deletequery, $conn);  
};
?>

When I click the delete button it appears to refresh the page but nothing changes. I imagine that if I can get the delete button working, the update will work in a similar fashion but for now I'm stumped.

  • 写回答

2条回答 默认 最新

  • dqt20140129 2016-05-01 14:48
    关注
    <?php
    if(isset($_POST['delete'])) {
        $deletequery = ("DELETE FROM lab WHERE **columnName** ='$_POST[id]'");
        mysql_query($deletequery, $conn);  
    }
    ?>
    

    You are missing column name in query. Also there is no * in DELETE statement, because deleting means deleting row.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!