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

时间:2015-09-02 12:00:02

标签: php html mysql database

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

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

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

更新: 我根据克里斯蒂安·哈瑞兰(Kristian Hareland)所写的内容进行了更改,并重新加载但是这个人没有被删除,应该修改哪些内容才能使其正常工作?

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:

<?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 个答案:

答案 0 :(得分:0)

我会在这里使用JavaScript和Ajax。

  1. 使用像delete_user();
  2. 这样的onclick函数创建一个Html按钮
  3. delete_user()调用.php文件来验证权限并执行一些mysql-delete promts。
  4. .php文件向JavaScript返回一个布尔值。
  5. 您可以捕获该布尔值并告知用户结果。

答案 1 :(得分:0)

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

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

并通过JQuery发送到那里:

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

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

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

和PHP:

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

答案 2 :(得分:0)

我会将GET方法用于此

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

在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}'";
    }
}

答案 3 :(得分:0)

只需使用链接:

   <?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");
}

DeletePerson.php:

int cursor = 0;
List<string> result = new List<string>();
while (cursor < parentArray.Length)
{
    result.Add(String.Join("", parentArray.Skip(cursor).Take(20)));
    cursor += 20;
}
string[] childArray = result.ToArray();