douhao7889 2016-07-18 01: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 01: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部