我有一个问题,我无法删除表中的行。当我选择一些东西并点击删除时,没有任何事情发生它只是刷新页面,我可以再次选择,但行仍然存在。我使用此页面上的代码:index.php?page = Delete
以下是我的代码:
try{
if($DBH == null)
$DBH = new PDO($dsn, $dbuser, $dbpass);
}
catch (PDOException $e){
echo '<b>PDOException: </b>',$e->getMessage();
die();
}
$action = empty($_GET['']) ? "" : $_GET[''];
if ($action == "") # No action specified so show the home page
{
try{
$sql = "select id, name, price from goods";
$STH = $DBH->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
echo "<form method='post' >";
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Price</th><th>Select</th></tr>";
while ($row = $STH->fetch())
{
echo "<tr><td><br>{$row["name"]} {$row["price"]} </td>";
echo "<td>";
echo "<input type='radio' value='{$row["id"]}' name='id2edit'>";
echo "</td></tr>";
}
echo "</table>";
echo "<br>";
echo "<input type='submit' value='Delete selected product' name='button'>";
echo "</form>";
$DBH = null;
}
catch (PDOException $e){
echo '<b>PDOException: </b>',$e->getMessage();
die();
}
}
else if ($action == "action='index.php?page=Delete'")
{
$id2edit = empty($_POST["id2edit"]) ? "" : $_POST["id2edit"];
if ($id2edit == "")
{
$m = "No goods selected! To return to the home ";
$m .= "screen click <a href='delcust.php'>here.</a>";
show_message($m);
}
else
{
try{
$STH = $DBH->prepare('delete from goods where id = :id');
$STH->bindParam(':id', $id2edit);
$STH->execute();
$DBH = null;
}
catch (PDOException $e){
echo '<b>PDOException: </b>',$e->getMessage();
die();
}
}
}
我现在已经走得更远了,但是表格中的值仍然没有删除:这是我现在的代码:
try{
# a DB Handler to manage the database connection
if($DBH == null)
$DBH = new PDO($dsn, $dbuser, $dbpass);
}
catch (PDOException $e){
echo '<b>PDOException: </b>',$e->getMessage();
die();
}
$action = empty($_GET['action']) ? "" : $_GET['action'];
if($action == "Delete"){
try{
$sql = "select id, name, price from goods";
$STH = $DBH->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
echo "<form method='post' action='delcust.php?action=delete_record'>";
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Price</th><th>Select</th></tr>";
while ($row = $STH->fetch())
{
echo "<tr><td><br>{$row["name"]} </td> <td> {$row["price"]} </td>";
echo "<td>";
echo "<input type='radio' value='{$row["id"]}' name='id2edit'>";
echo "</td></tr>";
}
echo "</table>";
echo "<br>";
echo "<input type='submit' value='Delete selected product' name='button'>";
echo "</form>";
$DBH = null;
}
catch (PDOException $e){
echo '<b>PDOException: </b>',$e->getMessage();
die();
}
}
else if ($action == "delete_record")
{
include("header.php");
include("nav.php");
include("aside.php");
$id2edit = empty($_POST["id2edit"]) ? "" : $_POST["id2edit"];
if ($id2edit == "")
{
$m = "No goods selected so far! Please select";
echo("$m");
}
else {
try{
$STH = $DBH->prepare('delete from goods where id = :id');
$STH->bindParam(':id', $id2edit);
$STH->execute();
echo "Goods with ID $id2edit deleted. :id To return to the home ";
echo "screen click <a href='index.php'>here.</a>";
$DBH = null;
}
catch (PDOException $e){
echo '<b>PDOException: </b>',$e->getMessage();
die();
}
include("footer.php");
}
}
?>
问题解决了:
$sql = "delete from goods where id = :id";
$STH = $DBH->prepare($sql);
现在可以删除值。
答案 0 :(得分:0)
if ($action == "")
始终为true,因为$_GET['']
始终未定义(并返回NULL)并且empty($_GET[''])
始终为true。
尝试以下代码:
try {
if ($DBH == null)
$DBH = new PDO($dsn, $dbuser, $dbpass);
} catch (PDOException $e) {
echo '<b>PDOException: </b>', $e->getMessage();
die();
}
$action = isset($_GET['page']) ? $_GET['page'] : '' ; // MODIFIED
if ($action == "Delete") { // MODIFIED
$id2edit = empty($_POST["id2edit"]) ? "" : $_POST["id2edit"];
if ($id2edit == "") {
$m = "No goods selected! To return to the home ";
$m .= "screen click <a href='delcust.php'>here.</a>";
show_message($m);
} else {
try {
$STH = $DBH->prepare('delete from goods where id = :id');
$STH->bindParam(':id', $id2edit);
$STH->execute();
} catch (PDOException $e) {
echo '<b>PDOException: </b>', $e->getMessage();
die();
}
}
}
try {
$sql = "select id, name, price from goods";
$STH = $DBH->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
echo "<form method='post' >";
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Price</th><th>Select</th></tr>";
while ($row = $STH->fetch()) {
echo "<tr><td><br>{$row["name"]} {$row["price"]} </td>";
echo "<td>";
echo "<input type='radio' value='{$row["id"]}' name='id2edit'>";
echo "</td></tr>";
}
echo "</table>";
echo "<br>";
echo "<input type='submit' value='Delete selected product' name='button'>";
echo "</form>";
$DBH = null;
} catch (PDOException $e) {
echo '<b>PDOException: </b>', $e->getMessage();
die();
}