douhao7889 2016-07-18 09:30
浏览 21
已采纳

Echo删除链接到php

I have a question regarding a delete function into php. Consindiring PHP I'm a real noob in this.

I want to make a delete link into a php loop.

This is my home.php:

while($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td class='info-item'>".$row["userID"]."</td>";
    echo "<td class='info-item'>".$row["userName"]." </td>";
    echo "<td class='info-item'>".$row["userLast"]."</td>";
    echo "<td class='info-item'>".$row["userDegree"]."</td>";
    echo "<td class='info-item'>".$row["userOrganization"]."</td>";
    echo "<td class='info-item'>".$row["userIndustry"]."</td>";
    echo "<td class='info-item'>".$row["userAddress-1"]."</td>";
    echo "<td class='info-item'>".$row["userAddress-1"]."</td>";
    echo "<td class='info-item'><a href='$delete'>Link</a></td>";
}

This line:

echo "<td class='info-item'><a href='$delete'>Link</a></td>";

is not working.

for $delete I use this on top of my script:

$delete = "delete.php";

It goes to delete.php that looks like this.

ob_start();
include("dbconfig.php");
if(isset($_GET['userID'])!="") {
    $delete=$_GET['userID'];
    $delete=mysql_query("DELETE FROM tbl_users WHERE userID='$delete'");
    if($delete)
        header("Location:index.php");
    else
        echo mysql_error();
}
ob_end_flush();

But nothing appears to happen regarding the delete of a certain id that I clicked.

Please help.

  • 写回答

4条回答 默认 最新

  • duanchazhou6779 2016-07-18 09:33
    关注

    Your delete is not working because it is redundant and static. You should make it Dynamic by (at least) appending the user ID to the delete.php. This id would then be used to determine which record to delete...

        <?php
            while($row = $result->fetch_assoc()) {
                // GENERATE AN ID BASED URL FOR THE DELETE
                // THIS WOULD READ SOMETHING LIKE: "delete?id=1" 
                // AND THE ID WOULD BE UNIQUE ACROSS EACH ROW....
                // YOU CAN THEN USE THE ID ($_GET['userID'] TO DETERMINE WHICH USER
                // ADD userId WHICH RECORD YOU HAVE TO DELETE....
                $delete = "delete.php?userID="  . $row["userID"];
                echo "<tr>";
                echo "<td class='info-item'>"   . $row["userID"]            . "</td>";
                echo "<td class='info-item'>"   . $row["userName"]          . " </td>";
                echo "<td class='info-item'>"   . $row["userLast"]          . "</td>";
                echo "<td class='info-item'>"   . $row["userDegree"]        . "</td>";
                echo "<td class='info-item'>"   . $row["userOrganization"]  . "</td>";
                echo "<td class='info-item'>"   . $row["userIndustry"]      . "</td>";
                echo "<td class='info-item'>"   . $row["userAddress-1"]     . "</td>";
                echo "<td class='info-item'>"   . $row["userAddress-1"]     . "</td>";
                echo "<td class='info-item'><a href='{$delete}' >Delete User</a></td>";
            }
    

    PHP:

        <?php
    
            // USING PDO...
            //DATABASE CONNECTION CONFIGURATION:
            defined("HOST")         or define("HOST",           "localhost");
            defined("DBASE")        or define("DBASE",          "yourDB");
            defined("USER")         or define("USER",           "root");
            defined("PASS")         or define("PASS",           "root");
    
            ob_start();
            if(isset($_GET['userID'])!="") {
                $userID         = $_GET['userID'];
                try {
                    $dbh        = new PDO('mysql:host='.HOST.';dbname='. DBASE,USER,PASS);
                    $dbh->setAttribute(PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    
                    $query      = "DELETE FROM `tbl_users` WHERE userID='{$userID}'";
                    $stmt       = $dbh->prepare($query);
                    $deleteOK   = $stmt->execute();
    
                    if ($deleteOK) {
                        header("Location:index.php");
                    }
    
                }catch(PDOException $e){
                    throw new Exception($e->getMessage());
                }
            }
            ob_end_flush();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)