简体   繁体   中英

SQL Remove From Table

I have a table that displays notices and has a clear option next to each row. However, when I press the clear button nothing happens. It currently runs using a button which is meant to trigger the clear function. What is the issue?

<button id="'.$row[0].'" class="btn-link" style="color: red;" value="'.$row[0].'" onclick="clearNotice('.$row[0].')">Clear</button>

function clearNotice($NoticeId)
{

    $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    if (!$link) {
        die('Could not connect: ' .mysql_error());
    }

    //First delete from calls list
    $query = "DELETE FROM notices WHERE id = ?";

    try {
        $stmt = mysqli_prepare($link, $query);
        mysqli_stmt_bind_param($stmt, "i", $NoticeId);
        $result = mysqli_stmt_execute($stmt);

        if ($result == FALSE) {
            die(mysqli_error($link));
        }
    }
    catch (Exception $e)
    {
        die("Failed to run query: " . $e->getMessage());
    }
}

PHP is server side. You would need to do something like:

<?php
function clearNotice($NoticeId)
{
   $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
   if (!$link) {
       die('Could not connect: ' .mysql_error());
   }

   //First delete from calls list
   $query = "DELETE FROM notices WHERE id = ?";
   try {
       $stmt = mysqli_prepare($link, $query);
       mysqli_stmt_bind_param($stmt, "i", $NoticeId);
       $result = mysqli_stmt_execute($stmt);

       if ($result == FALSE) {
           die(mysqli_error($link));
       }
   }
   catch (Exception $e)
   {
       die("Failed to run query: " . $e->getMessage());
   }
}

if(isset($_POST) && isset($_POST['delete-me'])){
   clearNotice($_POST['delete-me']);
}
// do whatever you do to get $row
?>
<form action="" method="post">
    <button name="delete-me" id="delete-<?php echo $row[0]; ?>" class="btn-link" style="color: red;" value="<?php echo $row[0] ?>">Clear</button>
</form>

Also note the change in ID due to the fact that you cannot have ID's starting with a number (assuming the ID is a number)

id="delete-<?php echo $row[0]; ?>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM