PHP& MySQL在同一页面上包含“删除"按钮 [英] PHP & MySQL Including a Delete button on same page

查看:78
本文介绍了PHP& MySQL在同一页面上包含“删除"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,非常感谢您关注我的问题.这有点是我第一个MySql/PHP项目之一,想知道是否有一种方法可以压缩我创建的这两页.

First and foremost thank you for taking a look at my question. This is somewhat one of my first MySql/PHP projects and wondering if there was a way to condense these two pages I have created.

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

Basically I have a MySql database with some dummy filler info in. I have created a php page that displays the info:

<?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",如下所示:

Based on the code sample, each row of data displays and there is a column for the delete button so I can delete a row when I want. However, the way it is set up now is it calls a secondary page "test_delete_ac.php" as shown below:

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


?>

一个出于可用性,我想限制它进入第二页,并且必须单击返回"链接.还有两个我可以创建一个函数并从链接中调用它以删除行并仍然显示表单吗?像页面重新加载之类的吗?

One for usability I want to limit it from going to a secondary page, and having to click the "go back" link. And two can I create a function and call it from the link to delete the row and still show the form? Like the page reloads or something?

在此先感谢您提供的任何重定向.

Thanks in advance for any redirection any of you may be able to offer.

-凯特

推荐答案

是的,只需在select语句上方添加删除逻辑即可.

Yes, just include the delete logic above your select statement.

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

?>

不过,请谨慎使用此方法.由于它只是一个链接,并且如果它是公共的,则漫游器可以爬网和删除所有内容.

Be careful with this approach, though. Since it's just a link, and if it's public, a bot can crawl and delete everything.

此外,请注意还有一些其他更改.为了避免歧义,我将参数从id更改为delete_id.我更新了删除链接.在运行删除查询之前,我正在检查是否已设置$_GET['delete_id'].而且,我在(c2)上强制转换(int)以确保它实际上是一个整数.

Also, note that there are a few other changes. I changed the param from id to delete_id, just to avoid ambiguity. I updated the delete link. I'm checking that $_GET['delete_id'] is set before running the delete query. And, I'm casting (int) on $_GET['delete_id'] to make sure it's actually an integer.

这篇关于PHP&amp; MySQL在同一页面上包含“删除"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆