PHP& MySQL在同一页面上包含删除按钮

时间:2016-03-30 19:21:54

标签: php mysql function

首先,感谢您查看我的问题。这是我的第一个MySql / PHP项目之一,并想知道是否有办法压缩我创建的这两个页面。

基本上我有一个带有一些虚拟填充信息的MySql数据库。我创建了一个显示信息的php页面:

<?php
// Connect to server and select database.
$link = mysqli_connect("localhost", "root", "password", "testdelete"); //for local machine testing

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// select record from mysql 
$sql = "SELECT id, name, lastname, email FROM testdelete.test_mysql";
$result = $link->query($sql);




if ($result->num_rows > 0) {

    include 'table_header.html';
     // output data of each row
     while($row = $result->fetch_assoc()) {

         echo "<tr>";
         echo "<td bgcolor=\"#FFFFFF\">".$row["id"]."</td>";
         echo "<td bgcolor=\"#FFFFFF\">". $row["name"]."</td>";
         echo "<td bgcolor=\"#FFFFFF\">" . $row["lastname"]."</td>";
         echo "<td bgcolor=\"#FFFFFF\">". $row["email"] . "</td>";
         echo "<td bgcolor=\"#FFFFFF\"><a href=\"test_delete_ac.php?id={$row['id']}\">Delete row</a></td>";
         echo "</tr>";

     }

     include 'table_footer.html';
} else {
     echo "0 results";
}



// close connection
mysqli_close($link);


?>

根据代码示例,显示每行数据,并且有一个删除按钮列,以便我可以在需要时删除一行。但是,它现在设置的方式是调用辅助页面“test_delete_ac.php”,如下所示:

<?php

// Connect to server and select database.
$link = mysqli_connect("localhost", "root", "password", "testdelete"); //for local machine testing

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// select record from mysql 
//$sql = "SELECT id, name, lastname, email FROM testdelete.test_mysql";
//$result = $link->query($sql);

// get value of id that sent from address bar 
$id=$_GET['id'];

// Delete data in mysql from row that has this id 
$sql="DELETE FROM testdelete.test_mysql WHERE id='$id'";
$result=$link->query($sql);

// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='delete.php'>Back to main page</a>";
}

else {
echo "ERROR";
}



// close connection
mysqli_close($link);


?>

一个可用性我希望限制它进入辅助页面,并且必须单击“返回”链接。我可以创建一个函数并从链接中调用它来删除行并仍然显示表单吗?像页面重装或什么?

提前感谢任何人可能提供的任何重定向。

-Kate

2 个答案:

答案 0 :(得分:2)

是的,只需在select语句上方包含删除逻辑。

<?php
// Connect to server and select database.
$link = mysqli_connect("localhost", "root", "password", "testdelete"); //for local machine testing

// Check connection
if ($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Handle delete
if (isset($_GET['delete_id'])) {
    // get value of id that sent from address bar 
    $delete_id = (int) $_GET['delete_id'];

    // Delete data in mysql from row that has this id 
    $sql="DELETE FROM testdelete.test_mysql WHERE id='$delete_id'";
    $result=$link->query($sql);

    // if successfully deleted
    if ($result){
        echo "Deleted Successfully";
        echo "<BR>";
    } else {
        echo "Delete ERROR";
    }
}

// select record from mysql 
$sql = "SELECT id, name, lastname, email FROM testdelete.test_mysql";
$result = $link->query($sql);

if ($result->num_rows > 0) {
    include 'table_header.html';
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td bgcolor=\"#FFFFFF\">".$row["id"]."</td>";
        echo "<td bgcolor=\"#FFFFFF\">". $row["name"]."</td>";
        echo "<td bgcolor=\"#FFFFFF\">" . $row["lastname"]."</td>";
        echo "<td bgcolor=\"#FFFFFF\">". $row["email"] . "</td>";
        echo "<td bgcolor=\"#FFFFFF\"><a href=\"?delete_id={$row['id']}\">Delete row</a></td>";
        echo "</tr>";
     }
     include 'table_footer.html';
} else {
     echo "0 results";
}

// close connection
mysqli_close($link);

?>

但要小心这种方法。由于它只是一个链接,如果它是公开的,机器人可以抓取并删除所有内容。

另外,请注意还有一些其他更改。我将参数从id更改为delete_id,以避免含糊不清。我更新了删除链接。我在运行删除查询之前检查$_GET['delete_id']是否已设置。而且,我在$_GET['delete_id']上施放(int)以确保它实际上是一个整数。

答案 1 :(得分:0)

你应该替换

echo "<td bgcolor=\"#FFFFFF\">
           <a href=\"test_delete_ac.php?id={$row['id']}\">Delete row</a>
      </td>";

使用此,但将current_page.php替换为列表中的页面。:

echo "<td bgcolor=\"#FFFFFF\">
           <a href=\"current_page.php?parem=delete&amp;id={$row['id']}\">Delete row</a>
      </td>";

并将其放在连接下方列表页面的顶部

  <?php
       if(isset($_GET['parem']) && $_GET['parem']=="delete"){
          $id=$_GET['id'];
         // Delete data in mysql from row that has this id 
         $sql="DELETE FROM testdelete.test_mysql WHERE id='$id'";
         $result=$link->query($sql);

         // if successfully deleted
         if($result){
             //redirect of the list page it is recommended// so use location to redirect to the list page
         }else {
         echo "ERROR";
         }
    }
?>
 // put the list code over here.

除了这些试图避免这样的事情,它不是一个好的做法。我建议使用课程。

Replace <td bgcolor=\"#FFFFFF\"> </td> with <td class='bgColor'> </td>