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


PHP & MySQL Including a Delete button on same page

首先感谢您关注我的问题。这在某种程度上是我的第一个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

是的,只需在select语句上方包含delete逻辑即可。

<?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),以确保它实际上是一个整数。

您应该更换

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.

除了这些尽量避免这样的事情,这不是一个好的做法。我建议使用class。

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