如何使用按钮从mysql中删除记录?

时间:2022-09-08 22:38:36

I would like to be able to delete a row using the delete-button I display at the end of each row. I need two queries to delete a person from my database completely since I have a n-m relationship between Persons and Functions. The queries are as follows:

我希望能够使用每行末尾显示的删除按钮删除一行。我需要两个查询才能从我的数据库中删除一个人,因为我在人与函数之间有一个n-m关系。查询如下:

delete from `Persons` where `Person ID` = '1';

I would like to implement these queries using the delete-button provided in the actual code, how can I do this?

我想使用实际代码中提供的删除按钮来实现这些查询,我该怎么做?

UPDATE: I made changes according to what Kristian Hareland wrote, and it reloads but the person isn't deleted, what should be changed to make it work?

更新:我根据克里斯蒂安·哈瑞兰写的内容进行了更改,然后重新加载,但是这个人没有删除,应该更改什么才能使其正常工作?

showall.php:

showall.php:

    <table>
<thead>
    <tr>
        <?php
// Variables
  $dbhost = "localhost";
  $dbuser = "root";
  $dbpass = "root";
  $dbname = "CISV";
  $dberror1 = "Could not connect to database: ";
  $dberror2 = "Could not select database: ";
  $dberror3 = "Could not execute query: ";

  $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1 . mysql_error());

  $select_db = mysql_select_db('CISV') or die ($dberror2 . mysql_error());

        $query = "SELECT p.`Person ID`, p.`First Name`, p.`Last Name`, p.Gender, p.`Date Of Birth`, p.Email, p.`Phone Number`, c.Region, c.Country, p.`Delegation ID`
        FROM Persons as p, Chapters as c
        WHERE c.`Chapter ID`=p.`Chapter ID`
        ORDER BY p.`Delegation ID";

        $result = mysql_query($query) or die($dberror3 . mysql_error());
        $row = mysql_fetch_assoc($result);
        foreach ($row as $col => $value) {
            echo "<th>";
            echo $col;
            echo "</th>";
        }
        ?>
    </tr>
</thead>
<tbody>
<?php

mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
    ?>
    <tr>
      <?php         
      foreach($row as $key => $value){
        echo "<td>";
        echo $value;
        echo "</td>";
    }
    ?>
    <td><a href="/DeletePerson.php?id=<?php echo $result['Person ID']; ?>" class="btn btn-danger">Delete</a></td>
</tr>
<?php } ?>

DeletePerson.php:

DeletePerson.php:

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "CISV";
$dberror1 = "Could not connect to database: ";
$dberror2 = "Could not select database: ";
$dberror3 = "Could not execute query: ";

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1 .      mysql_error());

$select_db = mysql_select_db('CISV') or die ($dberror2 . mysql_error());

$UserId = mysql_real_escape_string($_GET['id']);

if(isset($UserId)){
//DELETE QUERY
$Del = mysql_query("DELETE FROM `Persons` WHERE `Person ID` = '$UserId'");

if($Del){
    $Flag = TRUE;
}
else{
    $Flag = FALSE;
}

header("Location: /showall.php?delete=$Flag");


}
else{
die("Error");
}

4 个解决方案

#1


0  

I would use JavaScript and Ajax here.

我会在这里使用JavaScript和Ajax。

  1. Make a Html-button with an onclick function like delete_user();
  2. 使用像delete_user()这样的onclick函数创建一个Html按钮;
  3. delete_user() calls a .php file to validate the rights and execute some mysql-delete promts.
  4. delete_user()调用.php文件来验证权限并执行一些mysql-delete promts。
  5. The .php file returns a boolean to the JavaScript.
  6. .php文件向JavaScript返回一个布尔值。
  7. You could catch that boolean up and inform the user about the result.
  8. 您可以捕获该布尔值并通知用户结果。

#2


0  

IMHO best way is create separate file functions.php

恕我直言最好的方法是创建单独的文件functions.php

<?php
//functions.php
$id = filter_this($_POST[id]);
mysql_query("delete from Persons_Functions where `Person ID` = '$id');
?>

and send there via JQuery:

并通过JQuery发送到那里:

function deleteuser(id){
    $.post("admin_action.php", {action:'delete_user',id:id}, function(data){
    if(data.trim()=="done"){
        console.log("OK!");
    });
}}

Without JS You can use HTML (just change BUTTON to A):

没有JS您可以使用HTML(只需将BUTTON更改为A):

<a href='script.php?action=delete&id=12'>DELETE</a>

and PHP:

和PHP:

$id = $_GET[id];//filter this value
mysqli_query($link, "DELETE FROM users WHERE ID='$id'"); //use mysqli

#3


0  

I would use the GET method for this

我会使用GET方法

<?php foreach ($results as $result): ?>
    <a href="/index.php?action=delete&id=<?= $result['id']; ?>">Delete</a>
<?php endforeach; ?>

At the top of index.php

在index.php的顶部

if (isset($_GET['action']) && $_GET['action'] == 'delete') {
    if (isset($_GET['id']) && intval($_GET['id']) != 0) {
        $id = intval($_GET['id']);
        $stmt = "delete from table where id='{$id}'";
    }
}

#4


0  

Just use a link:

只需使用链接:

<tbody>
<?php

mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
    ?>
    <tr>
        <?php         
        foreach($row as $key => $value){
            echo "<td>";
            echo $value;
            echo "</td>";
        }
        ?>
        <td><a href="/DeletePerson.php?id=<?php echo $result['Person ID']; ?>" class="btn btn-danger">Delete</a></td>
    </tr>
    <?php } ?>
</tbody>

DeletePerson.php:

DeletePerson.php:

   <?php

$UserId = mysql_real_escape_string($_GET['id']);

if(isset($UserId)){
   //DELETE QUERY
  mysql_query("DELETE FROM Persons WHERE Person ID = '$UserId'");
  mysql_query("DELETE FROM Persons_Functions WHERE Person ID = '$UserId'");





  header("Location: /showall.php?flag=deleted");


}
else{
die("Error");
}

#1


0  

I would use JavaScript and Ajax here.

我会在这里使用JavaScript和Ajax。

  1. Make a Html-button with an onclick function like delete_user();
  2. 使用像delete_user()这样的onclick函数创建一个Html按钮;
  3. delete_user() calls a .php file to validate the rights and execute some mysql-delete promts.
  4. delete_user()调用.php文件来验证权限并执行一些mysql-delete promts。
  5. The .php file returns a boolean to the JavaScript.
  6. .php文件向JavaScript返回一个布尔值。
  7. You could catch that boolean up and inform the user about the result.
  8. 您可以捕获该布尔值并通知用户结果。

#2


0  

IMHO best way is create separate file functions.php

恕我直言最好的方法是创建单独的文件functions.php

<?php
//functions.php
$id = filter_this($_POST[id]);
mysql_query("delete from Persons_Functions where `Person ID` = '$id');
?>

and send there via JQuery:

并通过JQuery发送到那里:

function deleteuser(id){
    $.post("admin_action.php", {action:'delete_user',id:id}, function(data){
    if(data.trim()=="done"){
        console.log("OK!");
    });
}}

Without JS You can use HTML (just change BUTTON to A):

没有JS您可以使用HTML(只需将BUTTON更改为A):

<a href='script.php?action=delete&id=12'>DELETE</a>

and PHP:

和PHP:

$id = $_GET[id];//filter this value
mysqli_query($link, "DELETE FROM users WHERE ID='$id'"); //use mysqli

#3


0  

I would use the GET method for this

我会使用GET方法

<?php foreach ($results as $result): ?>
    <a href="/index.php?action=delete&id=<?= $result['id']; ?>">Delete</a>
<?php endforeach; ?>

At the top of index.php

在index.php的顶部

if (isset($_GET['action']) && $_GET['action'] == 'delete') {
    if (isset($_GET['id']) && intval($_GET['id']) != 0) {
        $id = intval($_GET['id']);
        $stmt = "delete from table where id='{$id}'";
    }
}

#4


0  

Just use a link:

只需使用链接:

<tbody>
<?php

mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
    ?>
    <tr>
        <?php         
        foreach($row as $key => $value){
            echo "<td>";
            echo $value;
            echo "</td>";
        }
        ?>
        <td><a href="/DeletePerson.php?id=<?php echo $result['Person ID']; ?>" class="btn btn-danger">Delete</a></td>
    </tr>
    <?php } ?>
</tbody>

DeletePerson.php:

DeletePerson.php:

   <?php

$UserId = mysql_real_escape_string($_GET['id']);

if(isset($UserId)){
   //DELETE QUERY
  mysql_query("DELETE FROM Persons WHERE Person ID = '$UserId'");
  mysql_query("DELETE FROM Persons_Functions WHERE Person ID = '$UserId'");





  header("Location: /showall.php?flag=deleted");


}
else{
die("Error");
}