如果您查看http://exhibitjewellery.com/editstocktest.php中的源代码,您将看到您的表单正在使用method="post"
<form action="/editstocktest.php?" method="post" name="form4" id="form4">
所以你不能我们$_GET
-
if (isset($_GET['delete'])) {}
另外,在您的删除链接中,您使用的是 的值delete
,但我认为您想要id
-$_POST['id']
最后,当您单击该链接时,将$_POST['id']
无法再在 中使用if (isset($_GET['yesdelete']))
,因此您需要将 添加id
到 url
此处的编辑 1
是更改它的一种建议方法,尽管根据 url 删除从来都不是一个好主意。最好确保只有管理员可以通过这种方式访问/删除。注意,GetSQLValueString()
是 Dreamweaver 创建的函数,因此您需要确保包含定义的文件。
<?php
if (isset($_POST['delete'])) {
echo 'Do you really want to delete ' . $_POST['piece'] . '? <a href="editstock.php?yesdelete=' . $_POST['id'] . '">Yes</a> | <a href="editstock.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {
include('file where GetSQLValueString() is defined'); // you have to change **file where GetSQLValueString() is defined** to the file path
$deleteSQL = sprintf("DELETE FROM pieces WHERE id='%s' LIMIT 1",
GetSQLValueString($_GET['yesdelete'], "int"));
mysql_select_db($database_connectmysql, $connectmysql);
$Result1 = mysql_query($deleteSQL, $connectmysql) or die(mysql_error());
header("location: editstock.php");
exit();
}
?>
编辑 #2
由于您想使用onclick
执行删除功能,您需要修改您的location.href
和使用$_GET
.
你的 html 按钮
<input name="delete" type="button" id="delete" value="delete" style="width:20%; float:right" onclick="window.location.href = '?delete=delete&id=<?php echo $row['id']; ?>&piece=<?php echo $row['piece']; ?>'"/>
我不知道你是如何填充你的表单元素的,所以改变$row['id']
你$row['piece']
的方法。
然后你的删除功能 -
<?php
if (isset($_GET['delete'])) {
echo 'Do you really want to delete ' . $_GET['piece'] . '? <a href="editstock.php?yesdelete=' . $_GET['id'] . '">Yes</a> | <a href="editstock.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {
include('file where GetSQLValueString() is defined'); // you have to change **file where GetSQLValueString() is defined** to the file path
$deleteSQL = sprintf("DELETE FROM pieces WHERE id='%s' LIMIT 1",
GetSQLValueString($_GET['yesdelete'], "int"));
mysql_select_db($database_connectmysql, $connectmysql);
$Result1 = mysql_query($deleteSQL, $connectmysql) or die(mysql_error());
header("location: editstock.php");
exit();
}
?>
编辑 3
如果您找不到 Dreamweaver 定义函数的位置,您可以重新声明它 -
<?php
if (isset($_GET['delete'])) {
echo 'Do you really want to delete ' . $_GET['piece'] . '? <a href="editstock.php?yesdelete=' . $_GET['id'] . '">Yes</a> | <a href="editstock.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {
// redefine if does not exist
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType,
$theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ?
mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$deleteSQL = sprintf("DELETE FROM pieces WHERE id='%s' LIMIT 1",
GetSQLValueString($_GET['yesdelete'], "int"));
mysql_select_db($database_connectmysql, $connectmysql);
$Result1 = mysql_query($deleteSQL, $connectmysql) or die(mysql_error());
header("location: editstock.php");
exit();
}
?>