我有一个关于将删除功能转换为php的问题。Consindiring PHP在这方面我是一个真正的菜鸟。
我想将删除链接添加到php循环中。
这是我的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>";
}
这行:
echo "<td class='info-item'><a href='$delete'>Link</a></td>";
不管用。
因为$delete
我在脚本之上使用了这个:
$delete = "delete.php";
看起来像这样的delete.php。
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();
但是,删除我单击的某个ID似乎没有任何反应。
请帮忙。
您的删除不起作用,因为它是冗余且静态的。您应该(至少)通过将用户ID附加到来使其变为动态delete.php
。这id
将被用来确定要删除的记录...
<?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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句